Javascript + Safari enigma

V

vendredi5h

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)) headerNodes.push(nodes);
}
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
 
M

Michael Winter

(e-mail address removed) 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)) headerNodes.push(nodes);
}
return headerNodes;
}


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
 
V

vendredi5h

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top