Boris Hamanov
Boris Hamanov

Reputation: 3195

Internet explorer 7/8 support of normal event passing, no need for window.event?

It is common knowledge that Internet explorer does not support event passing to event handler functions like this one:

function clickHandler(e) {
  // e is undefined in IE
  e = e || window.event;
{

For my surprise today, I found out that actually it does. I forgot to do this "e = e || window.event" trick in one of my functions, but it was working in IE8!

I did some tests with IE developer tools, the e object was fully defined and it was so even in IE7 mode.

My question is, should I drop the window.event stuff entirely since I do not care for IE versions prior to 8?

Upvotes: 2

Views: 1206

Answers (1)

Tim Down
Tim Down

Reputation: 324567

If you assign an event handler using the DOM0 property way, then you still need the e = e || window.event; bit and you'll get an error if you try and access a property of e:

document.onclick = function(e) {
    e.cancelBubble = true; // Error
};

If you use attachEvent then you're right, the event parameter is provided to the listener function:

document.attachEvent("onclick", function(e) {
    e.cancelBubble = true; // No error
});

Upvotes: 2

Related Questions