Prevent default event 'F1' in iE11

When the user presses the F1 key, I plan to display our application help and suppress the default action. I tried with various options so as not to show IE popup. Here is my code:

document.addEventListener('keydown', function (e) { if (e.key === 'F1' || e.keyCode == 112) { e.cancelBubble = true; e.cancelable = true; e.stopPropagation(); e.preventDefault(); e.returnValue = false; //my help menu code goes here } }); 

Please let me know how I can achieve by showing the help page of my application instead of IE. I am using version IE11.

+1
javascript jquery dom html internet-explorer
Mar 26 '15 at 12:39
source share
3 answers

You can subscribe to the window.onhelp event:

 window.onhelp =function() { alert(); return false; } 
+5
Mar 26 '15 at 12:49
source share

Try to do it

 <script> $(document).ready(function () { removedefaulthelp(); function removedefaulthelp() { window.onhelp = function () { return false; alert(); } } document.addEventListener('keydown', function (e) { if (e.key === 'F1' || e.keyCode == 112) { removedefaulthelp(); e.cancelBubble = true; e.cancelable = true; e.stopPropagation(); e.preventDefault(); e.returnValue = false; //my help menu code goes here } }); } </script> 

See more details.

+3
Mar 26 '15 at 12:57
source share

Here is an example similar to Sukanya , but my solution shows how to extend for F2 -F12 and intentionally ignores the combination keys F, such as CTRL + F1.

 <html> <head> <!-- Note: reference your own JQuery library here --> <script type="text/javascript" src="jquery-1.6.2.min.js"></script> </head> <body> <h1>F-key trap example</h1> <div><h2>Example: Press the 'F1' key to open help</h2></div> <script type="text/javascript"> //uncomment to prevent on startup //removeDefaultFunction(); /** Prevents the default function such as the help pop-up **/ function removeDefaultFunction() { window.onhelp = function () { return false; } } /** use keydown event and trap only the F-key, but not combinations with SHIFT/CTRL/ALT **/ $(window).bind('keydown', function(e) { //This is the F1 key code, but NOT with SHIFT/CTRL/ALT var keyCode = e.keyCode || e.which; if((keyCode == 112 || e.key == 'F1') && !(event.altKey ||event.ctrlKey || event.shiftKey || event.metaKey)) { // prevent code starts here: removeDefaultFunction(); e.cancelable = true; e.stopPropagation(); e.preventDefault(); e.returnValue = false; // Open help window here instead of alert alert('F1 Help key opened, ' + keyCode); } // Add other F-keys here: else if((keyCode == 113 || e.key == 'F2') && !(event.altKey ||event.ctrlKey || event.shiftKey || event.metaKey)) { // prevent code starts here: removeDefaultFunction(); e.cancelable = true; e.stopPropagation(); e.preventDefault(); e.returnValue = false; // Do something else for F2 alert('F2 key opened, ' + keyCode); } }); </script> </body> </html> 
0
Feb 22 '17 at 22:10
source share



All Articles