有没有办法使用Javascript在IE上捕获/覆盖Ctrl-R或F5? [英] Is there a way to capture/override Ctrl-R or F5 on IE using Javascript?

查看:334
本文介绍了有没有办法使用Javascript在IE上捕获/覆盖Ctrl-R或F5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to capture the Ctrl-R or F5 shortcut on the browser to prevent it from performing a browser refresh, but instead perform a custom refresh.

我可以使用以下方法在Safari和FF上捕获 Ctrl - R

I was able to capture Ctrl-R on Safari and FF using:

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

但这不适用于IE。有没有办法在IE上做这个?

But that doesn't work on IE. Is there any way to do this on IE?

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

推荐答案

打开JavaScript

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

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

For certain keys (F1, F4), you have to open a new browser window without the address bar.

示例

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

Open a new window, without adornments:


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

使用该库的JavaScript:

JavaScript to use the library:

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 );
}

内部 webpage.html

<body onload="disableShortcuts();">

这篇关于有没有办法使用Javascript在IE上捕获/覆盖Ctrl-R或F5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆