Safari Javascript, nodeTypes, help!

Discussion in 'Web Design and Development' started by A-J, Jul 13, 2007.

  1. A-J macrumors newbie

    Oct 18, 2005

    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 :)
  2. dcv macrumors G3

    May 24, 2005
    Just a guess but maybe try a regular for loop rather than as this might be a problem in Safari ??

    I've never tried a loop so might be talking nonsense.
  3. robbieduncan Moderator emeritus


    Jul 24, 2002
    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.


    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)
  4. pengu macrumors 6502a

    Mar 20, 2005
    Diddily Daddily...
    why are you even using JS to control it? use :hover and then run a bit of js on IE only to simulate :hover.

    SOOOO much simpler.
  5. dcv macrumors G3

    May 24, 2005
    Same here. Never used

    A quick Google just now came across this, which suggests that is a bit buggy in Safari.
  6. A-J thread starter macrumors newbie

    Oct 18, 2005
    Thanks for the quick replies.

    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.


Share This Page