Jump to content

for(x in array) on Safari


jesh

Recommended Posts

I conducted a test with the following code:

<html><body><form><input type="checkbox" id="check1" /><input type="checkbox" id="check2" /><input type="checkbox" id="check3" /><input type="checkbox" id="check4" /><input type="checkbox" id="check5" /></form><script type="text/javascript">var inputs = document.getElementsByTagName("input");for(var i in inputs){    alert("For-In: i = " + i);}for(var i = 0; i < inputs.length; i++){    alert("Typical For: i = " + i);}</script></body></html>

On IE in WindowsXP, and Firefox on XP and on the Mac, both for loops execute. However, on Safari, only the second for loop executes. Can someone please verify this for me and perhaps point me to a link which explains it in greater detail?Thanks!

Link to comment
Share on other sites

My recommendation is stick with good old for loops
I had only recently started trying out the for(i in array) loops but they don't quite act the same way as foreach(x in array) would in C# or PHP so I'll quit playing around with them and stick to the traditional for loops.
Link to comment
Share on other sites

this article explains one situation that can break for in loops http://www.andrewdupont.net/2006/05/18/jav...idered-harmful/If you use toolkits this could be an issue for you. I have just desided to avoid them, that way I am guarenteed to have no trouble with them :)
Nice link! I was just about to ask in a different post "Can you use strings as the keys/indices in arrays in javascript?" and this answers my question perfectly with "NO!". :) Yeah, I've never used the for in loops until recently. When I was first learning C#, I looked for a foreach equivalent in javascript but didn't find one so I stuck with the typical for loop. When I came across the for in loop, I figured that was my answer. But, like you, I'll just go back to sticking with the typical for loops from now on.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...