I'm not sure I understand why you want to include HTML pages with JavaScript. This is not very usual and there are probably other ways to do this. In any case if you insist on doing this you need to make sure, especially when doing this that you don't have a full document. So starter.html
should look more like this:
<p>Welcome to the homepage</p>
And page2.php
should look more like this:
<script>
function leggo() {
var ctx = document.getElementById('canvas').getContext('2d');
ctx.fillRect(0,0,100,100);
}
</script>
<canvas id="canvas" width="200px" height="200px">HI</canvas>
<script>
leggo();
</script>
Then you should add a call to the leggo()
at in xhttp.onreadystatechange
of loadPageBelow
like this:
<html>
<head>
<script>
function loadPageBelow(fileName) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
document.getElementById("content").innerHTML = xhttp.responseText;
};
xhttp.open("GET", fileName, true);
xhttp.send();
}
</script>
</head>
<body>
<p><b>THIS IS THE HEADER</b></p>
<a onclick="loadPageBelow('page2.php')">Click to goto page 2</a>
<div id="content">
<?php
include("starter.html");
?>
</div>
</body>
</html>