最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Canvas onload event isn't firing - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Mar 26, 2014 at 18:51 sigilsigil 9,54643 gold badges126 silver badges217 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 15

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

发布评论

评论列表(0)

  1. 暂无评论