Internet Explorer or any browser for F1 browser displays your own help

I would like to display the help file selected by the user when pressing F1. This should work in every browser where I am testing my application. How can I stop the display of the default help file?

+10
javascript internet-explorer
Aug 04 '10 at 12:21
source share
2 answers

AFAIK, the default action of the F1 key can be changed in any browser except IE. Microsoft teams are usually committed to maintaining a consistent user experience in their applications, and so F1 opens up help without returning false. There is a workaround in the form of a 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; } } 

The β€œother” step was adapted from a remote response, which was adapted from another answer , which, in turn, was adapted from another answer .

+15
Aug 04 '10 at 12:30
source share

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

Tested in IE8 and 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 event object window.event.keyCode = 0; } cancelKeypress = true; // Trigger custom help here alert("My help"); return false; } }); // Needed for Opera (as in Andy E answer) $(document).keypress(function ( evt ) { if ( cancelKeypress ) { cancelKeypress = false; // Only this keypress return false; } }); }); </script> </head> <body> </body> </html> 
+2
Feb 21 '12 at 17:39
source share



All Articles