IE: global variable not shown in for (var k in window) loop

R

runsun pan

This has been puzzling me for a long time.

Lets defined a global variable (not within any function) :

var fff = function(){}

This creates a global object that we can access via:

alert(fff)

or

window['fff']

We can even check this:

alert( 'fff' in window )

that gives us *true*.

However, if we try to locate this "fff" through a for loop:

for (var k in window) { blah blah blah }

It doesn't see "fff". This happens only in IE but not in Firefox.

Can anyone explain this, or better yet, show me how to do a correct
loop ?
 
R

RobG

runsun said:
This has been puzzling me for a long time.

Lets defined a global variable (not within any function) :

var fff = function(){}

This creates a global object that we can access via:

alert(fff) [...]
However, if we try to locate this "fff" through a for loop:

for (var k in window) { blah blah blah }

It doesn't see "fff". This happens only in IE but not in Firefox.

Can anyone explain this

Yes, it is a straight out flaw in IE:

<URL:
http://blogs.msdn.com/ericlippert/archive/2005/05/04/414684.aspx >
or better yet, show me how to do a correct loop ?

You can't use for..in to reliably find all the global variables in IE
that should be enumerable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top