Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  130] [ 4]  / answers: 1 / hits: 29272  / 10 Years ago, wed, march 26, 2014, 12:00:00

I can't get the onload event to work for a Canvas element. Here's the HTML:



<!DOCTYPE html>
<html>
<head>
<script src=js/main.js></script>
</head>
<body>
<canvas id=modelZone onload=initializeCanvas(); width=400 height=300></canvas>
</body>
</html>


And here's the Javascript in main.js:



function initializeCanvas(){
alert(hello);
}


I know that the HTML is finding main.js, because if I change onload to onclick it works correctly: clicking in the region of the Canvas triggers an alert. So why can't I do this with onload?


More From » html

 Answers
12

Only the body element (and perhaps iframe) can fire a onload event. Simply put <script> initializeCanvas(); </script> after the <canvas> tag.


[#71767] Tuesday, March 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shylaelisan

Total Points: 37
Total Questions: 94
Total Answers: 110

Location: Angola
Member since Tue, May 5, 2020
4 Years ago
;