Parse XML documents in JavaScript!

The problem on webpage was simple. Read XML file and process it. 1,2,3 and script was written, except it wasn’t working in IE. Ok, IE is an odd creature, but still. It wasn’t working even in IE 11!

The problem was .innerHTML property which (at first, I thought) was returning undefined, but then I found out that the method was undefined, it was not there!

JavaScript DOM object in IE didn’t have .innerHTML property! WTF

After few lost hours I found the bug!

If you parse XML string with this code:

return (new window.DOMParser().parseFromString(xmlStr, 'text/xml'))

you get totally different object than this one:

return (new window.DOMParser().parseFromString(xmlStr, 'text/html'))

If you use ‘text/html’ instead of ‘text/xml’ in IE everything works as expected. All objects (all nodes, elements …) will have all the expected properties!

As I said; few lost hours 🙁

Leave a Reply

You must be logged in to post a comment.