最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Internet Explorer or any Browser F1 keypress displays your own help - Stack Overflow

programmeradmin1浏览0评论

I would like to display the user selected help file when pressing F1. This should work on every browser where I test my application. How can I stop the default help file from being displayed?

I would like to display the user selected help file when pressing F1. This should work on every browser where I test my application. How can I stop the default help file from being displayed?

Share Improve this question edited Aug 4, 2010 at 12:26 Andy E 345k86 gold badges480 silver badges450 bronze badges asked Aug 4, 2010 at 12:21 DeveloperDeveloper 8,63643 gold badges136 silver badges249 bronze badges 5
  • “This should work on every browser where i test my application.” → Which are? – Marcel Korpel Commented Aug 4, 2010 at 12:23
  • The help file i supposed to open should work on every browser like Mozila , IE and other. If i press F1 on any browser the default help file should not be displayed – Developer Commented Aug 4, 2010 at 12:27
  • I'm assuming a question exists... but I cannot seem to find one. – Stephen Commented Aug 4, 2010 at 12:28
  • same concept: stackoverflow.com/questions/3286174/… includes solution for IE – lincolnk Commented Aug 4, 2010 at 13:15
  • @lincolnk – Not exactly; see Andy's answer regarding IE – Marcel Korpel Commented Aug 4, 2010 at 18:57
Add a comment  | 

2 Answers 2

Reset to default 16

AFAIK, the default action of the F1 key can be changed in any browser except for IE. The Microsoft teams are usually sticklers for maintaining a consistent user experience across their applications and that's why F1 opens help regardless of returning false. That being said, there's a workaround in the form of the window.onhelp event.

// Internet Explorer
if ("onhelp" in window)
    window.onhelp = function () { 
        showMyHelpInsteadOfTheUsualDefaultHelpWindow(true); 
        return false;
    }
// Others
else {
    document.onkeydown = function(evt) {
        cancelKeypress = (evt.keyCode == 112);
        if (cancelKeypress) {  // F1 was pressed
            showMyHelpInsteadOfTheUsualDefaultHelpWindow(true);
            return false;
        }
    }

    // Additional step required for Opera
    document.onkeypress = function(evt) {
        if (cancelKeypress) 
            return false;
    }
}

"Others" step was adapted from a deleted answer, which was adapted from another answer which, in turn, was adapted from another answer.

Actually, you can cancel the native Help in IE, by setting event.keyCode to 0:

Tested in IE8 & Chrome

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                var cancelKeypress = false;
                // Need to cancel event (only applies to IE)
                if ( "onhelp" in window ) {
                    // (jQuery cannot bind "onhelp" event)
                    window.onhelp = function () {
                        return false;
                    };
                }
                $(document).keydown(function ( evt ) {
                    // F1 pressed
                    if ( evt.keyCode === 112 ) {
                        if ( window.event ) {
                            // Write back to IE's event object
                            window.event.keyCode = 0;
                        }
                        cancelKeypress = true;

                        // Trigger custom help here
                        alert("My help");

                        return false;
                    }
                });
                // Needed for Opera (as in Andy E's answer)
                $(document).keypress(function ( evt ) {
                    if ( cancelKeypress ) {
                        cancelKeypress = false; // Only this keypress
                        return false;
                    }
                });
            });
        </script>
    </head>
    <body>

    </body>
</html>
发布评论

评论列表(0)

  1. 暂无评论