473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Javascript + Safari enigma

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
2 1538
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: joaopedrogoncalves | last post by:
Hi, I want to load an external javascript file, get its results and stick them inside a <div> block. I also want to do this in several places on a web page. This way the browser doesn't have...
3
by: Shiva | last post by:
This is my HTML Page containing javascript --------------------------------------------------- <html> <head> <script language="JavaScript"> var isSafari = (agent.indexOf("Safari") != -1);...
13
by: Christine Forber | last post by:
How can you make a div visible and hidden in safari? document.all.style.display = "none"; Does not appear to work. Also, can anyone tell me how to turn on javascript error commenting in...
2
by: laredotornado | last post by:
Hi, Is it possible to fool Javascript running on a Mac Safari web browser into believing it is a PC IE browser? We have the following JS code that is detecting both Mac and Safari. Sadly, we do...
3
by: Joe Cox | last post by:
I am having a problem with style properties for dynamic images in Mac OS X Safari. By dymanic images, I mean images allocated with the javascript 'new Image()' call. With static images (created...
7
by: petermichaux | last post by:
Hi, I have tried the following based on suggestions of the best way to insert JavaScript into a page. This is instead of using eval(). Unfortunately IE says "unexpected call to property or...
14
by: maidane | last post by:
Hi, I got the following situation using the AC_Quicktime library. Apple recommend to use a Javascript call to generate the OBJECT tag (http://developer.apple.com/internet/ieembedprep.html). I...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.