【发布时间】:2018-07-27 10:53:22
【问题描述】:
我正在尝试编写一个脚本,其中有一个请求 URL 并显示该页面的 HTML 的按钮,但我无法获取页面的 HTML,我想知道这是否可能。
目前,这是我的代码:
function injectHTML() {
//step 1: get the DOM object of the iframe.
var iframe = document.getElementById('test_iframe');
var html_string = prompt("Enter URL", "https://www.example.com");
var iframedoc = iframe.document;
if (iframe.contentDocument)
iframedoc = iframe.contentDocument;
else if (iframe.contentWindow)
iframedoc = iframe.contentWindow.document;
if (iframedoc) {
// Put the content in the iframe
iframedoc.open();
iframe.src = html_string
iframedoc.close();
} else {
//just in case of browsers that don't support the above properties.
alert('Cannot inject dynamic contents into iframe.');
}
}
<html>
<head>
</head>
<body>
<h1>Internet Page</h1>
<iframe id="test_iframe" src="about:blank" width=600 height=600></iframe>
<div class='search'>
<input id='search' type='button' value='Search ULR' onclick="javascript:injectHTML();" />
</div>
<style>
.search {
position: absolute;
top: 23px;
left: 530px;
}
</style>
</body>
<script language="javascript">
</script>
</html>
但它只显示互联网页面而不是 HTML 代码。
你能给我建议吗?
谢谢
【问题讨论】:
标签: javascript html iframe