Connecting Tech Pros Worldwide Help | Site Map

Mozilla Vs IE for childNodes.length

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 20th, 2006, 10:05 AM
Moses
Guest
 
Posts: n/a
Default Mozilla Vs IE for childNodes.length


HI

The Value for childNodes.length differs with mozilla and IE

Is it problem with my coding..... I could not under
stood.............


The following is the details

XML file

<track_order>
<status>0</status>
</track_order>

xmlDoc is the above XML

var n = xmlDoc.getElementsByTagName('track_order')[0];
alert(n.childNodes.length);

For Mozilla I get n.childNodes.length = 3

For IE I get n.childNodes.length = 1


Can any one please help

regards
moses


  #2  
Old October 20th, 2006, 11:55 AM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Mozilla Vs IE for childNodes.length

Moses wrote:
Quote:
The Value for childNodes.length differs with mozilla and IE
Quote:
<track_order>
<status>0</status>
</track_order>
>
xmlDoc is the above XML
>
var n = xmlDoc.getElementsByTagName('track_order')[0];
alert(n.childNodes.length);
>
For Mozilla I get n.childNodes.length = 3
>
For IE I get n.childNodes.length = 1
Mozilla for XML and HTML document includes whitespace between elements
as text nodes in the DOM, IE does not do that for HTML at all and for
XML does not do it by default (although you can set preserveWhiteSpace =
true on an MSXML DOMDocument).
So for Mozilla that track_order element has as its first child node a
text node with white space, then the status element as the second child
and then again a text node with white space as the third child.
MSXML (used by IE) only has the element node.

That should not pose a problem at all, if you are looking for child
elements then ignore the childNodes collection and simply use
getElementsByTagName (e.g.
xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]
gives you the element) and/or XPath to access the child elements. If you
still think you need to loop through childNodes then include checks for
nodeType being 1 for element nodes and 3 for text nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/
  #3  
Old October 20th, 2006, 12:25 PM
Moses
Guest
 
Posts: n/a
Default Re: Mozilla Vs IE for childNodes.length


Martin Honnen wrote:
Quote:
Moses wrote:
>
Quote:
The Value for childNodes.length differs with mozilla and IE
>
Quote:
<track_order>
<status>0</status>
</track_order>

xmlDoc is the above XML

var n = xmlDoc.getElementsByTagName('track_order')[0];
alert(n.childNodes.length);

For Mozilla I get n.childNodes.length = 3

For IE I get n.childNodes.length = 1
>
Mozilla for XML and HTML document includes whitespace between elements
as text nodes in the DOM, IE does not do that for HTML at all and for
XML does not do it by default (although you can set preserveWhiteSpace =
true on an MSXML DOMDocument).
So for Mozilla that track_order element has as its first child node a
text node with white space, then the status element as the second child
and then again a text node with white space as the third child.
MSXML (used by IE) only has the element node.
>
That should not pose a problem at all, if you are looking for child
elements then ignore the childNodes collection and simply use
getElementsByTagName (e.g.
xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]
gives you the element) and/or XPath to access the child elements. If you
still think you need to loop through childNodes then include checks for
nodeType being 1 for element nodes and 3 for text nodes.
>
--
>
Martin Honnen
http://JavaScript.FAQTs.com/


Hi

Thank you....

moses

  #4  
Old October 20th, 2006, 12:45 PM
Moses
Guest
 
Posts: n/a
Default Re: Mozilla Vs IE for childNodes.length

Hi
I solved the problem by

xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]

Since I am new to XML and want to learn more

Can u please give some more details on your last point because I
didnt understood it.


If you still think you need to loop through childNodes then include
checks for
nodeType being 1 for element nodes and 3 for text nodes.



regards
moses


Moses wrote:
Quote:
Martin Honnen wrote:
Quote:
Moses wrote:
Quote:
The Value for childNodes.length differs with mozilla and IE
Quote:
<track_order>
<status>0</status>
</track_order>
>
xmlDoc is the above XML
>
var n = xmlDoc.getElementsByTagName('track_order')[0];
alert(n.childNodes.length);
>
For Mozilla I get n.childNodes.length = 3
>
For IE I get n.childNodes.length = 1
Mozilla for XML and HTML document includes whitespace between elements
as text nodes in the DOM, IE does not do that for HTML at all and for
XML does not do it by default (although you can set preserveWhiteSpace =
true on an MSXML DOMDocument).
So for Mozilla that track_order element has as its first child node a
text node with white space, then the status element as the second child
and then again a text node with white space as the third child.
MSXML (used by IE) only has the element node.

That should not pose a problem at all, if you are looking for child
elements then ignore the childNodes collection and simply use
getElementsByTagName (e.g.
xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]
gives you the element) and/or XPath to access the child elements. If you
still think you need to loop through childNodes then include checks for
nodeType being 1 for element nodes and 3 for text nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/
>
>
>
Hi
>
Thank you....
>
moses
  #5  
Old October 20th, 2006, 01:25 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Mozilla Vs IE for childNodes.length

Moses wrote:
Quote:
I solved the problem by
>
xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]
Quote:
Can u please give some more details on your last point because I
didnt understood it.
>
>
If you still think you need to loop through childNodes then include
checks for
nodeType being 1 for element nodes and 3 for text nodes.
Assuming you did e.g.
var orderElement = xmlDoc.getElementsByTagName('track_order')[0];
you could loop through the childNodes collection looking for element
nodes e.g.
if (orderElement != null) {
for (var i = 0, l = orderElement.childNodes.length; i < l; i++) {
var child = orderElement.childNodes[i];
if (child.nodeType === 1) {
// you have an element node here
}
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
  #6  
Old October 20th, 2006, 01:55 PM
Moses
Guest
 
Posts: n/a
Default Re: Mozilla Vs IE for childNodes.length


Hi

Thank u very much for ur help I got it.......

regards
moses



Martin Honnen wrote:
Quote:
Moses wrote:
>
Quote:
I solved the problem by

xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]
>
>
Quote:
Can u please give some more details on your last point because I
didnt understood it.


If you still think you need to loop through childNodes then include
checks for
nodeType being 1 for element nodes and 3 for text nodes.
>
Assuming you did e.g.
var orderElement = xmlDoc.getElementsByTagName('track_order')[0];
you could loop through the childNodes collection looking for element
nodes e.g.
if (orderElement != null) {
for (var i = 0, l = orderElement.childNodes.length; i < l; i++) {
var child = orderElement.childNodes[i];
if (child.nodeType === 1) {
// you have an element node here
}
}
}
>
--
>
Martin Honnen
http://JavaScript.FAQTs.com/
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.