Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

weird IE 7 javascript problem

I have this javascript code working in firefox, chrome, and safari

for (idx in all_auction_ids){
    alert(all_auction_ids[idx]);
};

for the above, instead of getting the values in all_auction_ids, the first value I get is text of type function that looks like a for loop!

But if I run the code below, it works fine.

for (idx=0;idx<all_auction_ids.length;idx=idx+1){
    alert(all_auction_ids[idx]);
};

edit: updates

I did some debugging and found out that, adding Orbited and stomp.js is probably doing something with the array!

for now i am using Tracker1's suggestion jquery's $.each.

more info: http://groups.google.com/group/orbited-users/browse_thread/thread/7fd658cfb166e9fa

array with the problem http://bayimg.com/fAnhaAaBb

array without the problem http://bayimg.com/FaNhEAabb

like image 402
mark Avatar asked Mar 01 '23 01:03

mark


2 Answers

JavaScript's for/in construct is traditionally for iterating over object member names, not array indices. The more forward-thinking browsers have added features like hidden properties to help cases like Array enumerate in the way you would expect, but IE stilll does it the old-school way and gives you Object members like the 'toString' method when you use for/in over an Array.

The indexed-for is still the canonical JavaScript array loop. (Although you probably mean 'for (var idx=...', and 'idx++' is more common.)

like image 127
bobince Avatar answered Mar 08 '23 23:03

bobince


It's worth noting that some libraries such as prototype.js extend Array, so that they have additional properties beyond the internal indexes. This breaks for x in y notation beyond, as other mentioned, that IE will iterate properties. for i=0...i++ is preferred.

Also worth noting is jQuery, prototype and others offer a .each(fn) notation that I actually prefer.

like image 38
Tracker1 Avatar answered Mar 08 '23 22:03

Tracker1