More on IE and window.event

| 6 Comments

Julien Royer sent me an email today following up on this post. Try the following code in IE:

<script>
window.onload = function() {
  alert(window.event == window.event)
}
</script>

When I run this in IE6 on a Windows XP machine, I get false. This is just weird. The window.event object thinks that it is not equal to itself.

This probably does not have any impact on real-world code. I bring it up, however, because in that last post about IE and events, my commenters and I decided that the event object passed to an event handler registered with attachEvent is not the same as window.event. The code shown here calls that into question. Perhaps they are the same objects, but the weird behavior of window.event causes them to appear unequal.

6 Comments

Did you know that sometimes the window object is not equal to itself!? I think that the window/global object is constantly being cloned. I can't prove it but I feel it...

That's because they don't. I've come to believe that window.event in IE6 is some freakish one-time variable, almost as if it were a function doing something like:

window.event = function(){
var oldEvent = this;
var newEvent = ....; // create the new event
window.event = newEvent;
return oldEvent;
}

Why do I think this? Because of this code:

window.onload = function(){
var a = window.event;
var b = window.event;
var c = a;

a.foo = 'bar';
alert(a.foo+'\n'+b.foo+'\n'+c.foo);
}

Or rather it's a getter acting like that, which come to think of it is almost certainly what it -is-.

Looping through all the properties of the window.event object and it's sub objects all the property values seem to match.

I think that "event" is like a .Net attribute or a ActionScript 3 get function.

IE just creates a new Event object each time the "event" property of the global object is requested.

Dean is right: window is not always window in IE:

window.foo = function() { alert(this == window); }
foo();

Books

Comprehensive coverage of Ruby 1.8 and 1.9

"The New Most Important Ruby Book"
Peter Cooper,
rubyinside.com

Completely updated for Ajax and Web 2.0

"A must-have reference"
Brendan Eich,
creator of JavaScript

The classic Java quick-reference

Advertising

Pages

Hosted By

Powered by Movable Type 4.21-en