mark
mark

Reputation: 1217

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

Upvotes: 4

Views: 3029

Answers (5)

8paw
8paw

Reputation: 1

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)

Upvotes: 0

Ishmael
Ishmael

Reputation: 32620

This topic on the YUI blog is germane to your problem.

Upvotes: 0

Prestaul
Prestaul

Reputation: 85204

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?

Upvotes: 1

Tracker1
Tracker1

Reputation: 19344

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.

Upvotes: 2

bobince
bobince

Reputation: 536675

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.)

Upvotes: 5

Related Questions