How to disable default Help function of browsers

[亡魂溺海] 提交于 2019-11-30 17:33:49

问题


I want to disable or override the default help function of browsers.

I tried to look at few examples online but they are not working for me. (The following code is working on Firefox and Chrome but not on Opera and IE)

<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>


回答1:


I've found here this code that was clamied to work in each version of IE and 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>

Working JSFiddle. Note that you have to test it after clicking the result tab.



来源:https://stackoverflow.com/questions/9019278/how-to-disable-default-help-function-of-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!