Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Mozilla Vs IE for childNodes.length

Question posted by: Moses (Guest) on October 20th, 2006 10:05 AM

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

Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
October 20th, 2006
11:55 AM
#2

Re: Mozilla Vs IE for childNodes.length
Moses wrote:
Quote:
Originally Posted by
The Value for childNodes.length differs with mozilla and IE

Quote:
Originally Posted by
<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/

Moses's Avatar
Moses
Guest
n/a Posts
October 20th, 2006
12:25 PM
#3

Re: Mozilla Vs IE for childNodes.length

Martin Honnen wrote:
Quote:
Originally Posted by
Moses wrote:
>
Quote:
Originally Posted by
The Value for childNodes.length differs with mozilla and IE

>
Quote:
Originally Posted by
<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


Moses's Avatar
Moses
Guest
n/a Posts
October 20th, 2006
12:45 PM
#4

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:
Originally Posted by
Martin Honnen wrote:
Quote:
Originally Posted by
Moses wrote:
Quote:
Originally Posted by
The Value for childNodes.length differs with mozilla and IE

Quote:
Originally Posted by
<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



Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
October 20th, 2006
01:25 PM
#5

Re: Mozilla Vs IE for childNodes.length
Moses wrote:
Quote:
Originally Posted by
I solved the problem by
>
xmlDoc.getElementsByTagName('track_order')[0].getElementsByTagName('status')[0]


Quote:
Originally Posted by
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/

Moses's Avatar
Moses
Guest
n/a Posts
October 20th, 2006
01:55 PM
#6

Re: Mozilla Vs IE for childNodes.length

Hi

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

regards
moses



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

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

>
>
Quote:
Originally Posted by
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/



 
Not the answer you were looking for? Post your question . . .
180,788 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors