简体   繁体   中英

Prevent default key handling in IE9

I want to disable some default browser keys (CTRL + F, ALT + ENTER) as they do not make sense in my application scenario. What I've read so far the most important thing to disable these events in IE9 is to set the keyCode to 0 in this case, however I always get an "Access denied" error when I do this.

This is my code:

var fnOnKeyDown = function(event)
{
  if(!event)
  {
    event = window.event;
  }
  if(event.preventDefault)
  {
    event.preventDefault();
  }
  event.returnValue = false;
  event.cancelBubble = true;
  event.keyCode = 0;      
  return false;  
};

window.document.onkeydown = fnOnKeyDown;

As mentioned, the line event.keyCode = 0 throws an error "Access denied". When I remove it or put it into an empty try/catch block it doesn't throw the error but the default browser key handling is not suppressed anymore.

are you sure you don't mean ie7? I didn't seem to have a problem in ie9...

anyway, here is a post from microsoft on your issue

http://support.microsoft.com/kb/934364

To resolve this problem, change the code that assigns the event.keyCode property so that it does not change the value. The change in the code lets you use the SHIFT key or the CTRL key to load pages from the local disk

Alternatively, you can host the Web pages on a Web server. This makes sure that the event.keyCode property in the script runs correctly.

also, if possible I would advise the use of jquery. frameworks like that often handle various browser issues for you through a common interface.

event.preventDefault()event.stopPropagation()应该足够了 - 您不需要更改事件本身的任何属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM