I want to convert a Javascript DOM HTMLDcument to a string that I can write to a file. But how do you do the string conversion of the HTMLDocument to xml?!
Update If possible I'd like to see the html that is generated once any dynamic javascript rendering has been applied.
I want to convert a Javascript DOM HTMLDcument to a string that I can write to a file. But how do you do the string conversion of the HTMLDocument to xml?!
Update If possible I'd like to see the html that is generated once any dynamic javascript rendering has been applied.
Share Improve this question edited Nov 10, 2009 at 11:19 Joel asked Nov 2, 2009 at 22:38 JoelJoel 30.2k36 gold badges113 silver badges140 bronze badges3 Answers
Reset to default 12The DOM way of converting HTMLDocument object to XML is:
new XMLSerializer().serializeToString(oDocument);
In Internet Explorer there is no way to get proper XML representation of HTML document object by any built-in means. There you would need to implement serialization mechanism yourself - traversing the DOM tree and creating XML string.
'<html>'+document.documentElement.innerHTML+'</html>'
You could create a new div node, append the HTMLDocument as a child, and then read the innerHTML of the parent div, as shown below,
var div = document.createElement("div");
div.appendChild(oDocument);
console.log(div.innerHTML);