469,352 Members | 2,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Mozilla not displaying http_request data correctly, but IE is

I'm sure it's not mozilla's fault but I'm at a loss here.
I'm loading the following XML data into a page using
http_request:

<?xml version="1.0" encoding="iso-8859-1"?>
<forum>
<message id="msg1">
<date>10/13/2005</date>
<time>10:39 pm</time>
<name>Josh</name>
<comment>This is a message.</comment>
</message>
<message id="msg2">
<date>10/14/2005</date>
<time>11:00 pm</time>
<name>Jeff</name>
<comment>This is another message.</comment>
</message>
<message id="msg3">
<date>10/21/2005</date>
<time>3:21 pm</time>
<name>Jeff</name>
<comment>This is yet another message.</comment>
</message>
</forum>
The following functions retrieve the data and displays it on
the page:

function getmessages() {
var xmldoc = http_request.responseXML;
var
numberofmessages=xmldoc.getElementsByTagName('mess age').length
;
var root_node = xmldoc.getElementsByTagName('message');
for( var x = 0; x < numberofmessages; x++ ) {
var message =
root_node[x].childNodes[3].firstChild.nodeValue;
insertcontent(message);
var messagedatetime =
root_node[x].childNodes[2].firstChild.nodeValue + " [" +
root_node[x].childNodes[0].nodeName + ": " +
root_node[x].childNodes[0].firstChild.nodeValue + " -- " +
root_node[x].childNodes[1].nodeName + ": " +
root_node[x].childNodes[1].firstChild.nodeValue + "]" ;
insertcontent(messagedatetime);
}
insertcontent(numberofmessages);
}

function insertcontent(words) {
var ourDiv=document.getElementById('chatbox');
var ptag=document.createElement('p'); // create p tag
var chattext=document.createTextNode(words);// - create
the text to go in p tag
ptag.appendChild(chattext); // put text in p tag
ourDiv.appendChild(ptag); //put p tag in div
}

IE displays the results correctly:

This is a message.

Josh [date: 10/13/2005 -- time: 10:39 pm]

This is another message.

Jeff [date: 10/14/2005 -- time: 11:00 pm]

This is yet another message.

Jeff [date: 10/21/2005 -- time: 3:21 pm]

3
But Mozilla only shows:

10:39 pm

Any clues?????
Oct 26 '05 #1
2 1560


partridge wrote:
I'm sure it's not mozilla's fault but I'm at a loss here.
I'm loading the following XML data into a page using
http_request:

<?xml version="1.0" encoding="iso-8859-1"?>
<forum>
<message id="msg1">
<date>10/13/2005</date>
<time>10:39 pm</time>
<name>Josh</name>
<comment>This is a message.</comment>
</message>


Mozilla (and Opera too) by default includes white space between elements
as text nodes in the DOM while MSXML used by IE by default does not.
So any script trying to access an element as a child node at a certain
position (e.g. messageElement.childNodes[index]) will end up with
different nodes in the different implementations if the source XML is
formatted to have white space between elements, where MSXML/IE has an
element node Mozilla might have a text node.
Usually all you want is e.g.
messageElement.getElementsByTagName('date')[0]
that works whatever an implementation does with white space text.
But care needs to be taken if elements are nested recursively as
getElementsByTagName finds you all descendants while childNodes only the
descendants.
If you want to loop through childNodes then check
childNodes[index].nodeType to make sure you have an element.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 26 '05 #2
>

partridge wrote:
I'm sure it's not mozilla's fault but I'm at a loss
here. I'm loading the following XML data into a page
using http_request:

<?xml version="1.0" encoding="iso-8859-1"?>
<forum>
<message id="msg1">
<date>10/13/2005</date>
<time>10:39 pm</time>
<name>Josh</name>
<comment>This is a message.</comment>
</message>


Mozilla (and Opera too) by default includes white space
between elements as text nodes in the DOM while MSXML
used by IE by default does not. So any script trying to
access an element as a child node at a certain position
(e.g. messageElement.childNodes[index]) will end up with
different nodes in the different implementations if the
source XML is formatted to have white space between
elements, where MSXML/IE has an element node Mozilla
might have a text node. Usually all you want is e.g.
messageElement.getElementsByTagName('date')[0]
that works whatever an implementation does with white
space text. But care needs to be taken if elements are
nested recursively as getElementsByTagName finds you all
descendants while childNodes only the descendants.
If you want to loop through childNodes then check
childNodes[index].nodeType to make sure you have an
element.
--

Martin Honnen
http://JavaScript.FAQTs.com/


Excellent! Thank you. I totally forgotten about the white
space issue.
Oct 26 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Dennis Hueckelheim | last post: by
1 post views Thread by doug | last post: by
9 posts views Thread by Randall Sell | last post: by
1 post views Thread by James Pittman | last post: by
3 posts views Thread by rjones326 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.