tags:

views:

192

answers:

2

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.

A: 
'<html>'+document.documentElement.innerHTML+'</html>'
Joel
This is the short term solution I used, I'm hoping for a more comprehensive solution that writes out the html that is generated once the document has loaded and any dynamic javascript rendering has been applied. Sergey's solution below looks promising.
Joel
+2  A: 

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.

Sergey Ilinsky
Would this work in firefox though?
Joel
Yes, it would work in every browser but IE
Sergey Ilinsky
Would it display the rendered HTML? i.e. that which is rendered once the browser has loaded the document and applied any dynamic Javascript?
Joel
Joel, It is farely simple to test. To my understanding it should display the rendered HTML since the parameter of the serializeToStringfunction is the live DOM Node (document).
Sergey Ilinsky