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

标签 javascript html debugging

是否可以在 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/

相关文章:

javascript - Vueify + Elixir + 热加载 - 未捕获的类型错误 : Cannot read property 'indexOf' of undefined

html - 将表格放在可滚动 DIV 中时表格单元格换行?

javascript - 如何将数组结果显示为html

javascript - jquery 仅在我刷新页面以切换 Rails 应用程序时才起作用

javascript - 有没有办法移动 Chrome 调试器中的程序计数器?

javascript - Angular4 - 滚动超过 300px 时更改标题样式

javascript - jQuery 函数中的正则表达式语法

ios - 在...的对象上找不到属性 NSManagedObjects 上的错误调试属性

javascript - 使用带有 laravel 的 ajax 提交表单时避免页面重新加载

python - 显示一个实时的 numpy 数组