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

Show html code of web page when pressing an html button

本文关键字:html 代码 网页 按钮 按下 显示      更新时间:2023-09-26

在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);

有趣的想法!我所能想到的就是:

对于Chrome:尝试在另一个选项卡中打开url"查看源代码:[原始url]"。可能有效。

或者,使用php或ajax将html复制到txt文件中并链接到该文件?或者将其显示在块中或其他什么。。。

或者告诉用户按ctrl+U?