FieryA
FieryA

Reputation: 297

innerHTML after XMLHttpRequest not working on IE9

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 );
    }
});

Upvotes: 0

Views: 601

Answers (1)

Bergi
Bergi

Reputation: 664548

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 innerHTML properties2. You should use an XMLSerializer if you really need to get the markup of your xml document (maybe you're just looking for the .textContent?).

var el = list[0].childNodes[1];
console.log(new XMLSerializer().serializeToString(el));

However, oldIE doesn't even know that, you'll need to use el.xml for 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 innerHTML attribute on all DOM elements
3: see JavaScript: Replacement for XMLSerializer.serializeToString()?

Upvotes: 2

Related Questions