有没有办法使用Javascript在IE上捕获/覆盖Ctrl-R或F5?

air*_*tyh 8 javascript internet-explorer

我想捕获浏览器上的Ctrl- RF5快捷方式以防止它执行浏览器刷新,而是执行自定义刷新.

我能够捕获Ctrl- R在Safari和FF上使用:

document.onkeypress = function(e){
      if ((e.ctrlKey || e.metaKey) && e.keyCode == 114) // Ctrl-R
    e.preventDefault();
}
Run Code Online (Sandbox Code Playgroud)

但这对IE无效.在IE上有什么办法吗?

更新:对于那些问我为什么这样做的人:我只是想做一个自定义应用程序刷新,但是想避免使用"刷新"按钮,因为我有点不鼓励使用刷新(我们有一整页flex app).我们最终切换到F8,因为F5太难以在所有浏览器上运行.

Dav*_*vis 8

打开JavaScript

http://www.openjs.com/scripts/events/keyboard_shortcuts/

对于某些键(F1,F4),您必须打开一个没有地址栏的新浏览器窗口.

打开一个新窗口,没有装饰:

window.open( 'webpage.html', 'TLA', 
'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=665' );

JavaScript使用该库:

var FALSE_FUNCTION = new Function( "return false" );

/**
 * Called to disable F1, F3, and F5.
 */
function disableShortcuts() {
  // Disable online help (the F1 key).
  //
  document.onhelp = FALSE_FUNCTION;
  window.onhelp = FALSE_FUNCTION;

  // Disable the F1, F3 and F5 keys. Without this, browsers that have these
  // function keys assigned to a specific behaviour (i.e., opening a search
  // tab, or refreshing the page) will continue to execute that behaviour.
  //
  document.onkeydown = function disableKeys() {
    // Disable F1, F3 and F5 (112, 114 and 116, respectively).
    //
    if( typeof event != 'undefined' ) {
      if( (event.keyCode == 112) ||
          (event.keyCode == 114) ||
          (event.keyCode == 116) ) {
        event.keyCode = 0;
        return false;
      }
    }
  };

  // For good measure, assign F1, F3, and F5 to functions that do nothing.
  //
  shortcut.add( "f1", FALSE_FUNCTION );
  shortcut.add( "f3", FALSE_FUNCTION );
  shortcut.add( "f5", FALSE_FUNCTION );
}
Run Code Online (Sandbox Code Playgroud)

内部webpage.html:

<body onload="disableShortcuts();">
Run Code Online (Sandbox Code Playgroud)

  • 更好:`var FALSE_FUNCTION = function() { return false; };`。 (2认同)