Trouble link to external JS


hi,

 

i'm working through http://www.w3schools.com/html/html5_canvas.asp . want create seperate js rather include within html doc , can't examples work within seperate js. doesn't show of js. doing wrong?

 

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>html5 canvas</title>
    <script type="text/javascript" src="js/game.js"></script>
</head>
<body>

<canvas id="mycanvas" width="200" height="100" style="border:1px solid #c3c3c3;">
your browser not support html5 canvas tag.
</canvas>


</body>
</html>

 

game.js

 

// javascript document

// original w3c code...didn't work! var c=document.getelementbyid("mycanvas");

 

var mycanvas = document.getelementbyid("mycanvas");

var ctx=c.getcontext("2d");

ctx.fillstyle="#ff0000";

ctx.fillrect(0,0,150,75);

it doesn't work because external javascript file linked page before document object model (dom) loaded. javascript file trying use document.getelementbyid("mycanvas") before element id created.

 

put line before closing </body> tag:

 

<script type="text/javascript" src="js/game.js"></script>

 

then should work.



More discussions in Dreamweaver support forum


adobe

Comments

Popular posts from this blog

Falang and too many redirects - Joomla! Forum - community, help and support

Infinite loop detected in JErrorInfinite loop detected in JError - Joomla! Forum - community, help and support

logged out from joomla! - Joomla! Forum - community, help and support