I have the following black of code which is working perfectly fine on Chrome & Firefox but fails everytime on IE, it returns "undefined" in the console tab.
<html>
<head>
<script type="text/javascript" charset="utf-8" src="/js/jquery-latest.js"></script>
<script>
$(document).ready(function()
{
test();
});
function test()
{
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","/xml/products.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var list = xmlDoc.getElementsByTagName("product");
console.log(list[0].childNodes[1].innerHTML);
}
</script>
</head>
</html>
The XML i'm using is the following :
Thanks for your time.
EDIT: jQuery ajax version not working either:
var xmlDoc;
$.ajax({
type: "GET",
url: "/xml/products.xml",
dataType: "xml",
success: function (xml) {
xmlDoc = xml;
var list=xmlDoc.getElementsByTagName("product");
console.log(list[1].childNodes[1].innerHTML );
}
});
No idea why this works in Chrome and FF, it shouldn't actually1.
You are loading an XML document, and are successfully selecting an XML element node. Those don't have
innerHTMLproperties2. You should use anXMLSerializerif you really need to get the markup of your xml document (maybe you're just looking for the.textContent?).However, oldIE doesn't even know that, you'll need to use
el.xmlfor them3.1: At least not in older versions. See also does innerHTML work with XML Elements?.
2: Apparently, the DOM Parsing spec now includes a generic
innerHTMLattribute on all DOM elements3: see JavaScript: Replacement for XMLSerializer.serializeToString()?