The following code works fine in firefox but as with many other things, I cant get it working in Internet explorer (any version).
Can anybody help?
<body>
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","messages.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("entry");
for (i=0;i<x.length;i++)
{
document.write("<b>From:</b> ");
document.write(x[i].getElementsByTagName("username")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<b>Date:</b> ");
document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<b>Message:</b> ");
document.write(x[i].getElementsByTagName("message")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("<br />");
}
</script>
</body>
Two potential issues spring to mind:
Perhaps your AJAX communication is failing. In which case I would strongly urge you to use a library like JQuery to handle your communication. Then you don’t have to worry about the browser compatibility issues
Secondly, the data coming back from the server is not sent with
content-type("application/xml"). Different browsers may behave differently if they cannot reliably detect the content type and know that it’s XML that you’re expecting.[Edit]
This is a full working example that I tested in IE7
And the test data from
messages.xmlalso located at the root of my serverThis produces the following output
IE is a pain to debug because it has very limited debugging tools. I checked with FireFox’s FireBug add-on to see that the content-type from my WAMP server was “application/xml”.
You should also make sure that your XML document is valid by running it through a validator.