Internet Explorer exclusive Javascript error. Unable to get the value of the property 'childNodes'

ⅰ亾dé卋堺 提交于 2019-12-12 04:54:43

问题


ANSWERED: removing the header info from the XML file solved the issue. IE is great!

The following code works in Firefox and Chrome, not in IE.

I get the error

SCRIPT5007: Unable to get value of the property 'childNodes': object is null or undefined.  
line 106, character 3`

Line 106 is the first line inside the sort function, the one that begins 'var aCat = a.getElementsByTagName'....

None of the nodes being sorted are empty, although within the same in the xml file, there are some blank nodes (ie im sorting on , none of which are empty, but some people don't have phone number, or beeper numbers, etc. so some of those are empty)

When I tried putting the sort in a try-catch block IE just didn't read any of the nodes, they all threw an exception. (again, Chrome and Firefox worked with the try-catch block)

I'm stumped, any ideas?

function populateSection(listType, tableID ) {

if (window.XMLHttpRequest) {/*code for IE7+, Firefox, Chrome, Opera, Safari*/ xmlhttp=new XMLHttpRequest(); }
else {/* code for IE6, IE5*/ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }

var table = tableID;
var list = listType;

xmlhttp.open("GET",'contactlist.xml',false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

var xmlGet = xmlDoc.getElementsByTagName("PERSON");
var x = Array.prototype.slice.call(xmlGet,0);

x.sort(function(a,b) {
    var aCat = a.getElementsByTagName("LASTNAME")[0].childNodes[0].nodeValue;
    var bCat = b.getElementsByTagName("LASTNAME")[0].childNodes[0].nodeValue;
    if (aCat > bCat) return 1;
    if (aCat < bCat) return -1;
    return 0;
});

//then cycle through the array, adding table rows for each node
...}

EDIT: Here's a portion of the xml file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CONTACTLIST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <PERSON>
        <LASTNAME>ALLEN</LASTNAME>
        <FIRSTNAME>Korrie</FIRSTNAME>
        <EMAIL>redacted@evms.edu</EMAIL>
        <PHONE>123456</PHONE>
        <LIST>main</LIST>
    </PERSON>
    <PERSON>
        <LASTNAME>BUESCHER</LASTNAME>
        <FIRSTNAME>Chris</FIRSTNAME>
        <EMAIL>redacted</EMAIL>
        <LIST>main</LIST>
    </PERSON>
    <PERSON>
        <LASTNAME>BUESCHER</LASTNAME>
        <FIRSTNAME>Steve</FIRSTNAME>
        <EMAIL>redacted@evms.edu</EMAIL>
        <PHONE>123456</PHONE>
        <LIST>main</LIST>
        <LABOTHER>123456</LABOTHER>
        <BEEPER>123456</BEEPER>
    </PERSON>
    <PERSON>
        <LASTNAME>CHARTERS</LASTNAME>
        <FIRSTNAME>Michelle</FIRSTNAME>
        <EMAIL>redacted@evms.edu</EMAIL>
        <PHONE>123456</PHONE>
        <LIST>main</LIST>
    </PERSON>

回答1:


Since we aren't shown your XML, we're going to have a hard time figuring out where the code is breaking, but here are some guesses:

var x = Array.prototype.slice.call(xmlGet,0); This doesn't work in IE8, and may be buggy in IE9. Try alert(x) and see if the object is null.

If your XML starts with <?xml version="1.0" encoding="utf-8" ?> or similar, this can actual break in IE. I don't know why. Try removing it and seeing if it works.



来源:https://stackoverflow.com/questions/18277421/internet-explorer-exclusive-javascript-error-unable-to-get-the-value-of-the-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!