views:

188

answers:

2

The following declaration at the window level:

    var event; // for IE
    var event = "anything"; // for Chrome

will destroy the event object as used here:

    <div onMouseOver = "alert(event.type);">Mouseover Div</div>

Firefox does not seem phased by either declaration.

I realize that declaring a variable with the name "event" is bad code but I am curious about the technical difference here, e.g. that the use of var in IE reinitializes the variable to null, whereas Chrome will not overwrite with a var declaration unless a value is explicitly assigned, and maybe FF holds the event object outside of the window's var declaration scope altogether.

This is more of a curiosity. I ran into a bug in IE on a site outside of my control that was caused by this and the more I looked into the more I saw subtle differences between browsers. Just wondering if anyone had any insights here.

+2  A: 

The "event" object in IE is a property of the "window" object; that is, it's global. In Firefox, it's a value constructed and passed in to event handlers.

If you use jQuery or some other framework, usually the event handling support will normalize the "event" object into something that works identically across browsers.

Pointy
Thanks for the added information about frameworks normalizing the event object. Good insight.
johnmdonahue
If you like the answers you got, you should vote them up. That's how StackOverflow works for everybody!
Pointy
Thanks Pointy, I am new here and haven't gotten the required 15 points yet. But I will be sure to when I do.
johnmdonahue
+2  A: 

In IE, event is a property of the window object and is used in event handlers functions to access the event being handled. In other browsers such as Firefox, the situation is that in an event handler attribute, the JavaScript code inside the attribute is called as though it forms the body of a function into which has been passed a parameter called event that corresponds to the event being handled. So in

<div onmouseover="alert(event.type);">Mouseover Div</div>

the mouseover code is effectively

function(event) {
    alert(event.type);
}

and the event parameter overrides any event declared in a containing scope, whereas in IE, it's

function() {
    alert(event.type);
}

and the event identifier is resolved as a property of the global object (i.e. window).

Tim Down
Thanks for the explanation Tim - that makes perfect sense. So does Chrome handle the event object similar to IE? It seems to fail on: var event = null;. But this doesn't seem to be the case across WebKit as Safari seems to handle it fine.
johnmdonahue
I'm not sure yet, hence my vagueness in the phrase "other browsers such as Firefox". It would seem likely from what you say that Chrome's mechanism is similar to IE's, but I'll do some research.
Tim Down
It seems almost certain that Chrome is working the same way as IE: in an event handler attribute, `window.event === event` returns `true` and setting a global variable called `event` breaks things, since the global object is the last thing JavaScript checks when trying to resolve an identifier. The situation in Safari 4 is curious: `window.event === event` returns `true` but defining an `event` global variable does not override the value of `event` inside the event handler, so I'd conclude that Safari is both setting `window.event` and passing in the same object as the `event` parameter.
Tim Down
Thanks Tim. I appreciate you explaining your method here.
johnmdonahue