By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,273 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,273 IT Pros & Developers. It's quick & easy.

Javascript + Safari enigma

P: n/a
Hello,

Yesterday I spent a lot of time to find why my javascript script was
not working on Safari. I finaly found a solution but not the reason.
And I'd like to know if someone could tell me if what seems to be an
esotheric bug is actualy not!

Here is the problematic part:

-------------------------------
Index.prototype.getHeaderNodes = function(){
var nodes = this.node.childNodes;
var headerNodes = [];

// for(var i in nodes){ <-- Bad with Safari (v. 2.0.4)
for(var i = 0; i < nodes.length; i++){
if(this.isGoodNode(nodes[i])) headerNodes.push(nodes[i]);
}
return headerNodes;
}
-------------------------------

If I replace the "for(var i = 0; ..." for "for(var i in nodes){" it
simply not entering the for. As if nodes was empty.
I've got many "for(var i in nodes)" in my script and all of them are
working fine.
I did try to change "nodes" for "foo" to check if the variable name was
causing a problem but it didn't work either.

Testing with recent version of Firefox, IE, Mozilla are all working
fine with the line causing problem to Safari.

Thanks for you time.

Yannick

Oct 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ve********@gmail.com wrote:

[snip]
Index.prototype.getHeaderNodes = function(){
var nodes = this.node.childNodes;
var headerNodes = [];

// for(var i in nodes){ <-- Bad with Safari (v. 2.0.4)
for(var i = 0; i < nodes.length; i++){
if(this.isGoodNode(nodes[i])) headerNodes.push(nodes[i]);
}
return headerNodes;
}
-------------------------------

If I replace the "for(var i = 0; ..." for "for(var i in nodes){" it
simply not entering the for. As if nodes was empty.
I'm sure why you'd want to, anyway. A for..in statement enumerates
properties, and DOM collections have many. Nodes within that collection
aren't just obtained from ordinals, but from named properties as well
(the id and name attributes values of elements within that collection).
If you're only seeking to process all nodes in a collection, then surely
the obvious approach is to obtain the number of nodes (via the length
property) and access each numeric property in that range.

DOM collections are host objects. A host object may have as many or as
few enumerable properties as the host desires. If the collection is not
empty (the length property is greater than zero), but a for..in
statement doesn't iterate over any properties, then clearly none of
those properties are enumerable. This isn't a bug: it's perfectly
permissible implementation-defined behaviour.
I've got many "for(var i in nodes)" in my script and all of them are
working fine.
In Safari, or are you referring to other browsers?

[snip]

Mike
Oct 16 '06 #2

P: n/a

Michael Winter a écrit :
I'm sure why you'd want to, anyway. A for..in statement enumerates
properties, and DOM collections have many. Nodes within that collection
aren't just obtained from ordinals, but from named properties as well
(the id and name attributes values of elements within that collection).
If you're only seeking to process all nodes in a collection, then surely
the obvious approach is to obtain the number of nodes (via the length
property) and access each numeric property in that range.

DOM collections are host objects. A host object may have as many or as
few enumerable properties as the host desires. If the collection is not
empty (the length property is greater than zero), but a for..in
statement doesn't iterate over any properties, then clearly none of
those properties are enumerable. This isn't a bug: it's perfectly
permissible implementation-defined behaviour.
Thanks for your precisions. I wasn't using the for..in correctly. In
fact the way I used it can work but as you said the othre "for" option
should be the prefered one in this case.

I've got many "for(var i in nodes)" in my script and all of them are
working fine.

In Safari, or are you referring to other browsers?
Yes in Safari. I looked at the other places where I used for..in (all
of them to iterate over a nodes array). The difference with this one
was that the node array came from the element.childNodes and all other
places was node arrays that a created myself with Array.push(Node). So
I suspect that, since Arrays are object, maybe Safari set one or more
nonenumerable property and do iterate over array element only if there
is no existing property. Or something like that. Anyway I now
understand very well both for options. So time lost to make my script
working on Safari will be finally usefull. ;o)

Yannick

Oct 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.