473,804 Members | 3,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.child Nodes;
var headerNodes = [];

// for(var i in nodes){ <-- Bad with Safari (v. 2.0.4)
for(var i = 0; i < nodes.length; i++){
if(this.isGoodN ode(nodes[i])) headerNodes.pus h(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 1557
ve********@gmai l.com wrote:

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

// for(var i in nodes){ <-- Bad with Safari (v. 2.0.4)
for(var i = 0; i < nodes.length; i++){
if(this.isGoodN ode(nodes[i])) headerNodes.pus h(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.childNo des 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
5215
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 to wait for the external resource to load to show up the page, thus giving a perceiving faster load time for the user.
3
1677
by: Shiva | last post by:
This is my HTML Page containing javascript --------------------------------------------------- <html> <head> <script language="JavaScript"> var isSafari = (agent.indexOf("Safari") != -1); // LINE 1 var myimg = new Object(); // LINE 2
13
2892
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 Safari? Right now, if javascript fails or errors, the browser just sits there and doesn't return any error message.
2
1945
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 not have control over the code. // Global variables for platform branching var NN, IE, MAC, browserVersion
3
6696
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 with the html <img> tag), I can make the image visible or not, i.e. '<img style="visibility='hidden'" src='xxxx'/>'. But if I create the image dynamically with javascript: new Image() then try to modify the style, Safari chokes, and the Debug...
7
3965
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 method access" for the second to last line of my function. If you know what I've done wrong or how to fix it I would appreciate the help.
14
2311
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 am storing String entries representing the innerHTML of a div that I will update at runtime. As a result, some of these string entries are actually loooking like: <script language="JavaScript" type="text/javascript">
2
9043
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 figure this problem out. Any suggestions? thanks Joel
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.