Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 6th, 2005, 03:41 PM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default DOM childNodes property : IE and Mozilla

I'm new to the DOM and would like to clear up an inconsistancy between IE 6 and Mozilla 1.7.8. It seems the two browsers do not recognize the same number of child nodes/elements using the childNodes property. Why? The results of my script return the following...IE 6: 2 child nodes, First Node Type: 1, Second Node Type: 1
Mozilla 1.7.8:3 child nodes, First Node Type: 3, Second Node Type: 1
Code:
<html>
<head><title>Add paragraph to div</title>
<script language="javascript">

function Add_Content(){
   oBody = document.getElementsByTagName("body")[0];
   oDiv = oBody.getElementsByTagName("div")[0];
   addP = document.createElement("p");
   addText = document.createTextNode("New Paragraph of text added.");
   oDivChildren = oDiv.getElementsByTagName("p");

// If the div contains 2 or less "p" Elements add a new child "p" Element
if (oDivChildren.length <= 1)
 {
    addP.appendChild(addText);
    oDiv.appendChild(addP);
    oBody.appendChild(oDiv);
   }
 else
   { 
    alert ("The DIV tag contains " + oDiv.childNodes.length + " nodes.\n\n" 
+ "First Node Type: " + oDiv.childNodes[0].nodeType 
+ "\nSecond Node Type: " + oDiv.childNodes[1].nodeType);
   }
 }
// Mozilla 1.7.8 results:   3 nodes, First Node Type: 3, Second Node Type: 1
// IE 6 results:            2 nodes, First Node Type: 1, Second Node Type: 1
</script>
</head>
<body>
<p><a href="#" onclick="javascript:Add_Content();">Add</a> new content.</p>
<div>

</div>
</body>
</html>
 
Old June 6th, 2005, 07:53 PM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

After further review, I have figured out why a text node(nodeType: 3) mysteriously appears in my script. I left a space between the opening and closing DIV tags. By removing the space between the DIV tags the script returns the same values for IE and Mozilla. It looks like Mozilla handles whitespace differently than IE. ???

 
Old June 8th, 2005, 11:01 AM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

You are correct. Mozilla treats whitespace as a text node while IE ignores extra whitespace before or after an element or other type of node. Using childNodes is difficult because of this. You can fix your problem by making sure the DIV is completely empty, like this:

HTML Code:
<div></div>
No whitespace = no child nodes in either browser.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find no.of childnodes of a tag element? sachin635 .NET Framework 3.5 0 September 15th, 2008 04:32 AM
Conditional Output on childnodes? fahmyr XSLT 3 September 11th, 2006 11:49 AM
XMLHTTP and Mozilla dazzled XML 1 March 10th, 2006 06:22 AM
Mozilla don't support rickytang CSS Cascading Style Sheets 1 September 18th, 2004 03:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.