admin管理员组文章数量:1305232
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:
array with the problem
array without the 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./group/orbited-users/browse_thread/thread/7fd658cfb166e9fa
array with the problem http://bayimg./fAnhaAaBb
array without the problem http://bayimg./FaNhEAabb
Share Improve this question edited Jan 21, 2009 at 2:34 mark asked Jan 20, 2009 at 23:04 markmark 1,2174 gold badges12 silver badges17 bronze badges 1- What is all_auction_ids? How is it created? Do you get the same thing if you remove the semi-colon from the end of the "for" block? – Prestaul Commented Jan 20, 2009 at 23:15
5 Answers
Reset to default 5JavaScript'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 mon.)
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.
I agree with @bibince that you probably should be using the "for(var i = 0...
" syntax, but there is no reason that the syntax you chose should not work unless you have done something strange in your creation of all_auction_ids
. How are you initializing your array?
Arrays in JavaScript are just objects with a special auto-incrementing feature, but in reality they are not much different that an anonymous object. Try this in Firebug:
var a = ['a','b','c'];
a.d = 'd';
for(var i in a) console.log(i, a[i]);
or paste this into your address bar in IE and hit enter:
javascript:var a = ['a']; a.d = '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 = [];
rather than
var all_auction_ids = new Array();
If that doesn't help then could you post a little more of your code to give us a better idea of how you are populating all_auction_ids
?
This topic on the YUI blog is germane to your problem.
I've been having similar problems lately creating "select all / clear all" buttons for lists of checkboxes. In Firefox and Chrome they work fine, but in IE7/8 they don't. I'm not using any frameworks or external libraries, all the JavaScript is my own, it's fairly straightforward stuff, and there isn't much of it. I build the array of input elements using getElementsByTagName, then loop 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 perfectly with: for(ac=0;ac<allClearInputs.length;ac=ac+1){
Fails miserably with: for(var ac in allClearInputs)
本文标签: internet explorer 7weird IE 7 javascript problemStack Overflow
版权声明:本文标题:internet explorer 7 - weird IE 7 javascript problem - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741759062a2396291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论