简体   繁体   中英

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

No idea why this works in Chrome and FF, it shouldn't actually 1 .

You are loading an XML document, and are successfully selecting an XML element node. Those don't have innerHTML properties 2 . 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 them 3 .

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()?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM