按html按钮时显示网页的html代码

mic*_*ele 1 html javascript debugging

是否有可能在html/javascript中返回页面的html代码?

我有一个html页面,我想通过点击a input[type=button]打开页面的源代码.

Jam*_*ice 6

window.location = "view-source:" + window.location.href;
Run Code Online (Sandbox Code Playgroud)

这应该在Firefox和Chrome中运行良好.但它在Internet Explorer中不起作用.另一种选择是使用该outerHTML属性:

var source = document.documentElement.outerHTML;
Run Code Online (Sandbox Code Playgroud)

这应该适用于大多数浏览器.要在新窗口中打开它,您可以执行以下操作:

var sourceWindow = window.open();
sourceWindow.document.open('text/plain').write(document.documentElement.outerHTML);
Run Code Online (Sandbox Code Playgroud)