6

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?

7
  • You can better use jQuery instead of reinventing the wheel. It solves lots of different things with the awful browser called IE
    – Codebeat
    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.
    – User
    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
    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.
    – User
    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
    Mar 1, 2012 at 6:49

2 Answers 2

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);
    }
}
1
  • Thanks, this is also correct and more extensive but the other person was correct and first :)
    – User
    Feb 29, 2012 at 23:52
4

Use document.attachEvent instead. :]

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.