Safari Javascript, nodeTypes, help!

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

  1. A-J macrumors newbie

    Joined:
    Oct 18, 2005
    #1
    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 :)
     
  2. dcv macrumors G3

    Joined:
    May 24, 2005
    #2
    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.
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    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)
     
  4. pengu macrumors 6502a

    Joined:
    Mar 20, 2005
    Location:
    Diddily Daddily...
    #4
    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

    Joined:
    May 24, 2005
    #5
    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.
     
  6. A-J thread starter macrumors newbie

    Joined:
    Oct 18, 2005
    #6
    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.

    Thanks
     

Share This Page