I am trying to load HTML markup from a separate file placed in the same directory. To achieve that, I am following this article: HTML Includes That Work Today.
I have two files with the following contents:
page2.html
:
<html>
<style>
div {
display: inline-block;
background-color: #0902;
border: 2px solid green;
border-radius: 10px;
padding: 20px;
font-size: 1.2em;
}
</style>
<body>
<div id="x">Contents</div>
</body>
</html>
test.html
:
<html>
<body>
<iframe src="page2.html" style="display: none;" onload="alert('onload event called'); this.before(this.contentDocument.body.children[0]); this.remove();"></iframe>
</body>
</html>
I want the contents of page2.html
to get appended to the current document. But, I am getting:
Uncaught TypeError: Cannot read property 'body' of null
at HTMLIFrameElement.onload (test.html:10)
What mistake have I made?