Strange IE 7 javascript issue

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 the text of a function of the type that looks like a for!

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 probably does something with an array!

I now use the Tracker1 jquery $ .each clause.

Additional information: http://groups.google.com/group/orbited-users/browse_thread/thread/7fd658cfb166e9fa

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

array without problems http://bayimg.com/FaNhEAabb

+4
source share
5 answers

JavaScript for the / in construct is traditionally used to iterate over the names of the members of an object, not the indexes of an array. More future-oriented browsers have added features such as hidden properties so that cases like Array are listed as you expected, but IE stilll does this with the old school method and provides you with Object elements such as the toString method when you use to / in over the array.

The indexed value is still the canonical loop of the JavaScript array. (Although you probably mean "for ( var idx = ...", but "idx ++" is more common.)

+5
source

It is worth noting that some libraries, such as prototype.js, extend Array, so that they have additional properties outside of the internal indexes. This is interrupted to indicate x in y outside, as mentioned above, that IE will iterate over the properties. for i = 0 ... i ++ is preferred.

It's also worth noting that jQuery, prototype, and others offer the .each (fn) notation, which I really prefer.

+2
source

I agree with @bibince that you should probably use the syntax " for(var i = 0... ", but there is no reason why your chosen syntax should not work unless you have done something strange in all_auction_ids you create all_auction_ids How do you initialize your array?

Arrays in JavaScript are just objects with a special auto-increment function, but in reality they are not much different from an anonymous object. Try this on Firebug:

 var a = ['a','b','c']; ad = 'd'; for(var i in a) console.log(i, a[i]); 

or paste this into your address bar in IE and press enter:

 javascript:var a = ['a']; ad = 'd'; for(var i in a) alert(a[i]); alert(a.length); 

EDIT:

I doubt this is your problem, but do you have the same problem if you use:

 var all_auction_ids = []; 

but not

 var all_auction_ids = new Array(); 

If this does not help, can you post a little more of your code to give us a better idea of ​​how you populate all_auction_ids ?

+1
source

This section of the YUI blog is relevant to your problem.

0
source

I have been experiencing similar issues lately by creating "select all / clear all" buttons for checkboxes. In Firefox and Chrome, they work fine, but in IE7 / 8 they do not work. I do not use any frameworks or external libraries, all JavaScript is my own, these are pretty simple things, and they are few. I create an array of input elements using getElementsByTagName, then scroll through:

 var allClearInputs = document.getElementsByTagName("input"); for(ac=0;ac<allClearInputs.length;ac=ac+1){ if(allClearInputs[ac].id){ var thisNameArr = allClearInputs[ac].id.split("-"); var thisName = thisNameArr[0]; if(thisName == checkName){ if((actionType == 'all' && allClearInputs[ac].checked == false) || (actionType == 'clear' && allClearInputs[ac].checked == true)){ allClearInputs[ac].click(); } } } } 

works fine: for (ac = 0; ac <allClearInputs.length; ac = ac + 1) {
Error failed: for (var ac in allClearInputs)

0
source

All Articles