Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts

    Wrong childeNode.nodeName in IE?

    Okay I was coding some DOM earlier today. I noticed that IE and FF have a different DOM structure. For example
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <style type="text/css">
    div {
    background:#F00;
    color:#FFF;
    border:1px solid #000;
    }
    </style>
    </head>
    
    <body>
    <div onclick="alert(this.childNodes[1].nodeName)">
    <input type="radio" />
    </div>
    </body>
    </html>
    Clicking on the div in FF will alert INPUT but in IE it will alert #text. Am I doing something wrong or is this just the way IE interprets the document? Anyway to get the same results in IE and FF?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah Mozilla and IE have inconsistent DOMs with respect to whitespace text nodes. There's a number of ways you can deal with this:

    - don't rely on child relationships, use iterators like getElementsByTagName,

    - do node conversion by checking for node names and converting references to next or previous sibling as appropriate

    - normalize the section of DOM you're interested in by removing all useless whitespace

    The third method is a process overhead to do it in the first place, but it you gives you a nice predictable stucture afterwards. Check out this script by Alex - http://www.codingforums.com/showthread.php?t=7028

    Note however that you can't do this in Mac/IE5 or it will undermine the visual rendering - in mac/ie5 nested structures collapse when stripped of whitespace. But if you're lucky you don't have to think about that
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may also rely on the nodeType attribute while using a while loop through the childNodes
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Thanks guys. Alex's script seemed to work well.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •