Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does innerHTML work with XML Elements?

According to JavaScript: The Definitive Guide: "Despite its name, innerHTML can be used with XML elements as well as HTML elements".

However, when I actually attempt to access the innerHTML property of an XML Element object, undefined is returned:

var xml = $.ajax({url: "test.xml", async: false}).responseXML.documentElement;
console.log(xml.innerHTML); // displays "undefined" in console log

What is the explanation for this behavior?

test.xml:

<?xml version="1.0" encoding="utf-8"?>
<foo><bar>baz</bar></foo>
like image 667
Josh Avatar asked Dec 04 '22 08:12

Josh


1 Answers

The earlier answers don't address the newer browsers that exist as of September 2014. When an XML document is created with:

var parser = new DOMParser(); 
var doc = parser.parseFromString(data, "text/xml");

where data is the XML document as a string, then the following are true:

  • In Firefox 28, and Chrome 36, the nodes in doc have an innerHTML field that contains the XML serialization of the contents of the node.

  • In IE 9, 10 and 11, Safari 7.0 and Opera 12, the nodes in doc have neither an innerHTML field nor an xml field. I was not able to identify something that could stand for these fields. There does not appear to be any other option than using XMLSerializer for these browsers.

like image 75
Louis Avatar answered Dec 05 '22 22:12

Louis