Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-13-2012, 05:07 PM   PM User | #1
docco
New Coder

 
Join Date: Aug 2012
Posts: 67
Thanks: 18
Thanked 0 Times in 0 Posts
docco is an unknown quantity at this point
A note of Child in MSIE and Crome

With the same method to calculate the number of Child in DOM, I have the different results in MSIE and in Crome. MSIE throws the right result while Crome throws the wrong. Please check the code below to see:

Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>calculate Child Number</title>
</head>
<body>
<script type="text/javascript"> 
itemarray = new Array(); 
itemarray[100] = 'This is Item 100'; 

function addtocart(ref) { 
var parentel = document.getElementById('itemlist'); 
newrow = document.createElement('div'); 
newrow.innerHTML = itemarray[ref.id]; 
parentel.appendChild(newrow); 
} 
function getParentLength() {
var parente = document.getElementById('itemlist');
var nodes=parente.childNodes;
alert(nodes.length);
}
</script> 
<p><button id="100" onclick="addtocart(this);return false;">add Hat 100</button></p>
<p><button onclick="getParentLength()">Check number of Child</button></p>
<h2>Your cart:</h2> 
<div id="itemlist"> 
</div> 
</body>
</html>
Anyone have any idea?
docco is offline   Reply With Quote
Old 09-13-2012, 05:34 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 809
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Yes the numbers are different
but they are both correct,
browsers other than IE add
an empty text node to
empty divs

Code:
 
function getParentLength() {
 var parente = document.getElementById('itemlist');
 var nodes=parente.childNodes;
 alert(nodes.length);
 a="";
 for(var i=0; i<nodes.length; i++)
  a += nodes[i].nodeType +"\n"
 alert(a)
}
DaveyErwin is offline   Reply With Quote
Old 09-13-2012, 10:28 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
If you want to get the same answer in all browsers when counting nodes then you need to get rid of all the blank text nodes first and also combine any adjacent text nodes into single text nodes.

See http://javascriptexample.net/dom36.php for a function that you can run that will do this for you.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 09-13-2012, 10:38 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
depending on what you are trying to do, you may be better testing for children:

Code:
function getParentLength() {
var parente = document.getElementById('itemlist');
var chil=parente.children;
alert(chil.length);
}
(returns consistent in IE, Chrome and FF, except IE<9 counts an inline comment in the html as 1...)

Last edited by xelawho; 09-13-2012 at 10:44 PM.. Reason: IE disclaimer
xelawho is offline   Reply With Quote
Old 09-15-2012, 02:20 AM   PM User | #5
rdspoons
New Coder

 
Join Date: Jun 2009
Posts: 81
Thanks: 0
Thanked 8 Times in 8 Posts
rdspoons is on a distinguished road
felgall,

PM was disabled. The normalizeAll function code located at http://javascriptexample.net/dom36.php
has unballanced parentheses in the first conditional. Maybe a type-0?

The conditional in question is repeated below for clarity:
Code:
if((3 === c.nodeType && !/\S/.test(c.nodeValue))) || 8 === c.nodeType) {
	node.removeChild(child);
}
rdspoons is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 AM.


Advertisement
Log in to turn off these ads.