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

前端之家收集整理的这篇文章主要介绍了有没有办法使用Javascript捕获/覆盖IE上的Ctrl-R或F5?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想捕获浏览器上的Ctrl-R或F5快捷方式,以防止浏览器刷新,而是执行自定义刷新.

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

  1. document.onkeypress = function(e){
  2. if ((e.ctrlKey || e.MetaKey) && e.keyCode == 114) // Ctrl-R
  3. e.preventDefault();
  4. }

但是这并不适用于IE.有没有办法在IE上这样做?

更新:对于那些正在问我为什么这样做的人,首先我只是想做一个自定义的应用程序刷新,但是想避免有一个“刷新”按钮,因为我有点不鼓励使用刷新(我们有一个完整的页面flex应用).我们最终转向F8,因为F5太难以在所有浏览器上工作.

解决方法

打开JavaScript

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

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

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

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

JavaScript使用库:

  1. var FALSE_FUNCTION = new Function( "return false" );
  2.  
  3. /**
  4. * Called to disable F1,F3,and F5.
  5. */
  6. function disableShortcuts() {
  7. // Disable online help (the F1 key).
  8. //
  9. document.onhelp = FALSE_FUNCTION;
  10. window.onhelp = FALSE_FUNCTION;
  11.  
  12. // Disable the F1,F3 and F5 keys. Without this,browsers that have these
  13. // function keys assigned to a specific behavIoUr (i.e.,opening a search
  14. // tab,or refreshing the page) will continue to execute that behavIoUr.
  15. //
  16. document.onkeydown = function disableKeys() {
  17. // Disable F1,F3 and F5 (112,114 and 116,respectively).
  18. //
  19. if( typeof event != 'undefined' ) {
  20. if( (event.keyCode == 112) ||
  21. (event.keyCode == 114) ||
  22. (event.keyCode == 116) ) {
  23. event.keyCode = 0;
  24. return false;
  25. }
  26. }
  27. };
  28.  
  29. // For good measure,assign F1,and F5 to functions that do nothing.
  30. //
  31. shortcut.add( "f1",FALSE_FUNCTION );
  32. shortcut.add( "f3",FALSE_FUNCTION );
  33. shortcut.add( "f5",FALSE_FUNCTION );
  34. }

inside webpage.html:

  1. <body onload="disableShortcuts();">

猜你在找的JavaScript相关文章