Converting HTMLDocument to a printable string
I want to convert a Javascript DOM HTMLDcument to a string that I can write to a file开发者_JAVA百科. 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.
The 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);
精彩评论