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

javascript - Why keydown listener doesn't work in IE - Stack Overflow

programmeradmin0浏览0评论

Im trying to run this in IE 8 but it doesn't work, any idea? It works in Firefox, Chrome, Opera...

preventBackspace();

function preventBackspace() {
    try {
        if (window.addEventListener) {
            window.addEventListener("keydown", onKeyDown, true);
        } else if (window.attachEvent) { // IE 
            alert(window);
            window.attachEvent("onkeydown", onKeyDown);
        } else {
            document.addEventListener("keydown", onKeyDown, true);
        }
        } catch (e) {
            alert(e);
    }
}

function onKeyDown(e) {
    alert("test!");
}

jsfiddle:

/

window.attachEvent is defined and the event listener added. But it never shows "test!" alert.

I read something about useCapture flag, which is possible to use in the other methods. It captures the key press on the window before the event "goes down". Internet Explorer doesn't seem to allow/use this. Is that the problem? If yes, how can I solve it?

Im trying to run this in IE 8 but it doesn't work, any idea? It works in Firefox, Chrome, Opera...

preventBackspace();

function preventBackspace() {
    try {
        if (window.addEventListener) {
            window.addEventListener("keydown", onKeyDown, true);
        } else if (window.attachEvent) { // IE 
            alert(window);
            window.attachEvent("onkeydown", onKeyDown);
        } else {
            document.addEventListener("keydown", onKeyDown, true);
        }
        } catch (e) {
            alert(e);
    }
}

function onKeyDown(e) {
    alert("test!");
}

jsfiddle:

http://jsfiddle.net/ubfBq/

window.attachEvent is defined and the event listener added. But it never shows "test!" alert.

I read something about useCapture flag, which is possible to use in the other methods. It captures the key press on the window before the event "goes down". Internet Explorer doesn't seem to allow/use this. Is that the problem? If yes, how can I solve it?

Share Improve this question asked Feb 29, 2012 at 21:48 ixxixx 32.3k41 gold badges137 silver badges237 bronze badges 7
  • You can better use jQuery instead of reinventing the wheel. It solves lots of different things with the awful browser called IE – Codebeat Commented Feb 29, 2012 at 22:06
  • Yes the thing is that this is a very very small javascript section inside a flex project and I don't want to load JQuery just for few methods. – ixx Commented Feb 29, 2012 at 22:12
  • Ok, but i think that is a false argument and you create a problem yourself when a solution is already there. – Codebeat Commented Feb 29, 2012 at 22:33
  • Why false, if I see it makes sense to use JQuery I will, but I'm not going to add a library just because 1 or 2 functions. – ixx Commented Feb 29, 2012 at 23:55
  • I know what you are saying and i understand the reason but as i know everything starts 'little' and can grow in the future because it needs more functionality. What will you do, you add a framework. Problem with this is extra code that was not needed when using a framework (you will get different implementations of same things on the long run). That's all i want to say about it. I have seen it too many times. So many companies rewrote there software because it was a mess after several years. – Codebeat Commented Mar 1, 2012 at 6:49
 |  Show 2 more comments

2 Answers 2

Reset to default 12

It appears that only IE9 and later support binding keydown on window: http://www.quirksmode.org/dom/events/keys.html#t00

Instead, bind it to the document for IE:

function preventBackspace() {
    try {
        if (window.addEventListener) {
            window.addEventListener("keydown", onKeyDown, true);
        } else if (document.attachEvent) { // IE 
            alert(document);
            document.attachEvent("onkeydown", onKeyDown);
        } else {
            document.addEventListener("keydown", onKeyDown, true);
        }
    } catch (e) {
        alert(e);
    }
}

Use document.attachEvent instead. :]

发布评论

评论列表(0)

  1. 暂无评论