I'm trying to write a script where there is a button that asks for a URL and shows the HTML of that page, but I can't manage to get the HTML of the page and I wonder if it is possible.
For the moment, here is my code:
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>
But it only shows the internet page and not the HTML code.
Can you give me advice?
Thanks