Reputation: 23
I'm parsing through an XML file from an XMLHttpRequest:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
loadItems(this);
}
};
xhttp.open("GET", "articles.xml", true);
xhttp.send();
function loadItems(xml) {
var xmlDoc = xml.responseXML;
var items = xmlDoc.getElementsByTagName("item");
for(var i = 0; i < items.length; i++){
//Next line is where the script crashes...
var newCategory = articles[i].parentElement.nodeName;
alert(newCategory);
}
}
I've tracked down my bug to the line with:
var newCategory = articles[i].parentElement.nodeName;
The XML structure I'm working with:
<head>
<category>
<item>
<title>
foo
</title>
<contents>
bar
</contents>
</item>
</category>
<newcategory>
<item>
<title>
left
</title>
<contents>
right
</contents>
</item>
</newcategory>
</head>
This seems to work in every other browser except Internet Explorer where the script silently crashes at this line. In other browsers newCategory will have a string "category" or "newcategory" depending on the parent node of each item.
I've looked at Windows' docs and they say both parentElement and nodeName accessors are supported so why would make the script crash in IE?
Upvotes: 0
Views: 111
Reputation: 23
After doing some research into the Internet Explorer docs, I found that there is a parentNode accessor which will work in Firefox/Chrome/Opera and Explorer.
var newCategory = articles[i].parentNode.nodeName;
Upvotes: 1
Reputation:
I had a similar problem. Try using .nodeValue
instead of .nodeName
.
var newCategory = articles[i].parentElement.nodeValue;
Upvotes: 0