Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas onload event isn't firing

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?

like image 291
sigil Avatar asked Mar 26 '14 18:03

sigil


1 Answers

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

like image 157
Jonas Äppelgran Avatar answered Nov 06 '22 13:11

Jonas Äppelgran