4
document.onkeydown = function(event) {
    var tagName = event.target.tagName;
    if (tagName != 'INPUT' && tagName != 'TEXTAREA' && !event.alt && event.control) {

        if (event.ctrlKey && event.keyCode == 37) {
            if (_this.currentPage > 1) {
                window.location.href = _this.baseUrl.replace(/%page%/i, _this.currentPage + 1);
            }
        } else if (event.ctrlKey && event.keyCode == 39) {
            if (_this.currentPage < _this.pagesTotal) {
                window.location.href = _this.baseUrl.replace(/%page%/i, _this.currentPage - 1);
            }
        }
    }
}

This gives me an error only in IE 8:

'target' is null or not an object

for that line var tagName = event.target.tagName;

How to fix that? Error happens when I press Ctrl or arrows button.

4
  • event.target does not exist in IE8. Welcome to IE's old event system. Commented Nov 12, 2012 at 2:06
  • Please can you give an example how to fix that? Commented Nov 12, 2012 at 2:09
  • Use a third-party event API what takes care of the cross-browser incompatibilities for you. That would be my advice. Commented Nov 12, 2012 at 2:11
  • Yeah, but I am not good at all in Javascript. I need example what to change. Commented Nov 12, 2012 at 2:13

2 Answers 2

6

IE does not pass in the event object into the event handler. Instead, they use the global event property of the window object. So for IE, you'd use window.event instead.

It is common practice to test for the supplied argument first. You also have to take into account the fact the IE uses srcElement instead of target. To account for all that, use something similar to this:

document.onkeydown = function(event) {
    event = event || window.event;
    var tagName = (event.target || event.srcElement).tagName;
    // Keep up the good work...
}

This should do the trick.

Sign up to request clarification or add additional context in comments.

5 Comments

For a second there it looked like a direct copy/paste. Why remove the toUpperCase?
You know I tried that before. It didn't help me. Then it gives the same error on the next line var tagName = event.target.tagName;.
@user1689607 - I just don't think people should still be using quirks mode. I definitely hope the OP is using a proper doctype. AFAIK, when in standards mode, IE will return the element's name in uppercase. Someone correct me if I'm wrong.
There are a couple edge cases for IE (I don't remember which versions).
Thank you! The additional answer below fix the problem.
3

Do it like this:

event = event || window.event;
var tagName = (event.target || event.srcElement).tagName.toUpperCase();

1 Comment

The same then it gives the same error on the next line var tagName = event.target.tagName.toUpperCase();.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.