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>