是否可以在 html/javascript 中返回页面的 html 代码?
我有一个 html 页面,我想通过单击 input[type=button]
打开该页面的源代码。
最佳答案
window.location = "view-source:" + window.location.href;
这在 Firefox 和 Chrome 中应该可以正常工作。但它在 Internet Explorer 中不起作用。另一种选择是使用 outerHTML
属性:
var source = document.documentElement.outerHTML;
这在大多数浏览器中应该都能正常工作。要在新窗口中打开它,您可以这样做:
var sourceWindow = window.open();
sourceWindow.document.open('text/plain').write(document.documentElement.outerHTML);
关于javascript - 按下 html 按钮时显示网页的 html 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6562912/