我想捕获浏览器上的Ctrl-R或F5快捷方式,以防止浏览器刷新,而是执行自定义刷新.
我能够在Safari和FF上捕获Ctrl-R:
document.onkeypress = function(e){ if ((e.ctrlKey || e.MetaKey) && e.keyCode == 114) // Ctrl-R e.preventDefault(); }
但是这并不适用于IE.有没有办法在IE上这样做?
更新:对于那些正在问我为什么这样做的人,首先我只是想做一个自定义的应用程序刷新,但是想避免有一个“刷新”按钮,因为我有点不鼓励使用刷新(我们有一个完整的页面flex应用).我们最终转向F8,因为F5太难以在所有浏览器上工作.
解决方法@H_301_12@
打开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,and F5 to functions that do nothing.
//
shortcut.add( "f1",FALSE_FUNCTION );
shortcut.add( "f3",FALSE_FUNCTION );
shortcut.add( "f5",FALSE_FUNCTION );
}
inside webpage.html:
<body onload="disableShortcuts();">
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,and F5 to functions that do nothing. // shortcut.add( "f1",FALSE_FUNCTION ); shortcut.add( "f3",FALSE_FUNCTION ); shortcut.add( "f5",FALSE_FUNCTION ); }
inside webpage.html:
<body onload="disableShortcuts();">