最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting HTMLDocument to a printable string - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a comment  | 

3 Answers 3

Reset to default 12

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);
发布评论

评论列表(0)

  1. 暂无评论