![](/img/trans.png)
[英]How do I get the source HTML for a page after executing its associated JavaScript?
[英]How do I get the HTML source from the page?
有没有办法使用JavaScript访问页面HTML源代码?
我知道我可以使用document.body.innerHTML
但它只包含正文中的代码。 我想获取所有页面源代码,包括头部和身体标签及其内容,如果可能的话,还有html标签和doctype。 可能吗?
使用
document.documentElement.outerHTML
要么
document.documentElement.innerHTML
这可以使用XMLSerializer在单行中完成。
var generatedSource = new XMLSerializer().serializeToString(document);
哪个给String
<!DOCTYPE html><html><head>
<title>html - javascript page source code - Stack Overflow</title>
...
一种方法是使用XMLHttpRequest重新请求页面,然后您将从Web服务器逐字获取整个页面。
只要
页面源可以重新下载:
fetch(document.location.href)
.then(response => response.text())
.then(pageSource => /* ... */)
对于IE,您还可以使用:document.all [0] .outerHTML
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.