2012-01-26 52 views
4

我想禁用或覆蓋瀏覽器的默認幫助功能。如何禁用瀏覽器的默認幫助功能

我試着在網上看幾個例子,但他們不適合我。 (下面的代碼正在FF和鉻,但不是歌劇&即)

<html> 
<title> 
</title> 
<body> 
    <script src = "jquery-1.7.1.min.js" text="type="text/javascript""></script> 
    <script language="javascript" type="text/javascript">   
      document.onkeydown = function(event) 
      { 
       if(window.event && window.event.keyCode == 112) 
       { 
        event.stopPropagation(); 
        event.preventDefault(); 
        event.keyCode = 0; 
        return false; 
        //document.onhelp = new Function("return false;"); 
        //window.onhelp = new Function("return false;"); 
        //helpFunction(); 

       } 
       else if(event.which == 112) 
       { 
        helpFunction(); 
       } 
      }; 
      var false_function = new function(){"return false";}; 
      shortcut.add("f1",false_function); 
     var helpFunction = function() { 
      alert('help'); 
     } 
    </script> 
    <h2>Test</h2> 
</body> 

+0

如何將此添加到我的瀏覽器? – erjoalgo

回答

10

我發現here這段代碼中被聲明所​​有權在IE瀏覽器的每個版本的工作和FF

<script type="text/javascript"> 

function avoidInvalidKeyStorkes(evtArg) { 
    var evt = (document.all ? window.event : evtArg); 
    var isIE = (document.all ? true : false); 
    var KEYCODE = (document.all ? window.event.keyCode : evtArg.which); 

    var element = (document.all ? window.event.srcElement : evtArg.target); 
    var msg = "We have disabled this key: " + KEYCODE; 

    if (KEYCODE == "112") { 
     if (isIE) { 
      document.onhelp = function() { 
       return (false); 
      }; 
      window.onhelp = function() { 
       return (false); 
      }; 
     } 
     evt.returnValue = false; 
     evt.keyCode = 0; 
     window.status = msg; 
     evt.preventDefault(); 
     evt.stopPropagation(); 
     alert(msg); 
    } 

    window.status = "Done";  
}  

if (window.document.addEventListener) { 
    window.document.addEventListener("keydown", avoidInvalidKeyStorkes, false); 
} else { 
    window.document.attachEvent("onkeydown", avoidInvalidKeyStorkes); 
    document.captureEvents(Event.KEYDOWN); 
} 

</script> 

工作JSFiddle。請注意,點擊結果選項卡後,您必須對其進行測試。