Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

A-J

macrumors newbie
Original poster
Oct 18, 2005
20
0
Hi

I've created a drop-down menu for a site I'm working on, the menu is built using nested <ul> elements, and uses javascript to control it. (collapse / expand when a menu item is clicked).

It works fine in Firefox and IE, but I'm having trouble getting the javascript to work in Safari.

I think its breaking on this bit of code

for(var i in nav.childNodes){
if(nav.childNodes.nodeType == 1 && nav.childNodes.nodeName.toLowerCase() =='ul'){
theItem = nav.childNodes;
// code removed
}
}

When I alert nav.childNodes.nodeType it comes out as undefined, so not sure if Safari handles childNodes in a different way.

Any ideas? Cheers, Andy :)
 
for(var i in nav.childNodes)

Just a guess but maybe try a regular for loop rather than for...in as this might be a problem in Safari ??


I've never tried a for...in loop so might be talking nonsense.
 
Just a guess but maybe try a regular for loop rather than for...in as this might be a problem in Safari ??


I've never tried a for...in loop so might be talking nonsense.

I used to write tons of JS and I've never seen that construct, but I've just looked it up in my huge book of JS and it's an official part of the language so should work.

A-J

Have you checked:
1) nav is actually assigned? If you alert it what do you get
2) nav actually has child nodes (although if it did not the loop would simply get executed 0 times)
 
I used to write tons of JS and I've never seen that construct, but I've just looked it up in my huge book of JS and it's an official part of the language so should work.

Same here. Never used for...in

A quick Google just now came across this, which suggests that for...in is a bit buggy in Safari.
 
Thanks for the quick replies.

1) nav is actually assigned? If you alert it what do you get

nav is a div that the menu is contained within, when I do an alert I get [object HTMLDivElement]

I'm still fairly new to JavaScript, so any help is great.

Thanks
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.