The Window Object in IE is Actually an Array of Frames

Posted by Tejus Parikh on March 26, 2008

I happened to come across this javascript quirk in a very obscure way today. I was maintaining some code that when presented with a string such as this.foo.bar, would evaluate it, if a complex variable such as that exists in the supplied context, or just return the string if it does not. It does this lookup by evaluating each piece with an array lookup. In this example, the first step would be to perform, window['this'].

This code works pretty well in Safari and Firefox, but in IE6, I got this strange error that just didn’t make any sense. For some reason, the symbol lookup code was changing the string ‘5’ to the window object. What I really couldn’t understand was why removing the Appcelerator Calendar widget fixed the problem.

The supplied context to my variable lookup guy was window. Of course, window[5] doesn’t mean anything. But Wait! IE6 returns something that looks an awful lot like the window object. On a whim, I tried, window[1000] which returned undefined, which is what I was expecting in the first place. Then I looked a little harder at the DOM that resulted from the widget and noticed <iframe> tags. I had 5 of these widgets, which would explain why windows 0-5 weren’t undefined. If the value I was trying to test this code on wasn’t 2, I would have never seen this.

In the end, my fix was, if the value to lookup looks like a number, assume the person wants a number. As is normally the cause with debugging, 5 hours of work results in a 3 line change.

Related Posts:

Tejus Parikh

Tejus is an software developer, now working at large companies. Find out when I write new posts on twitter, via RSS or subscribe to the newsletter: