I have a javascript file that is being called by 2 html files, I need the javascript to edit the canvas that is on 1.html and 2.html, this doesn't work unless I put both canvasses in the same html file. Is there a way to work around this problem, here is the code:
HTML1
<body>
<canvas class="canvas" id="canvas1"></canvas>
</body
</html>
HTML2
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<canvas class="canvas" id="canvas2"></canvas>
</body
</html>
Javascript:
for (var i = 1; i<3; i++) {
var c=document.getElementById("canvas"+[i]);
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
}
This doesn't work unless I put both canvasses in the same html file, then it works
EDIT: Please note that I do want the code to be running in the background at all times, updating the htlm files I'm not in. The filling and clearing is just a placeholder for my code, which is not part of the problem
canvas1doesn't exist in HTML2 andcanvas2doesn't exist in HTML1. You need to check before using the canvas.