I prepare a project with a simple webpage, canvas, and javascript files that run game in the canvas. I want load my code when page starts, what is best way?
- put body onload?
- startu开发者_如何学Gop code in body of .js file (not in function)?
- attach to window load event?
- use some jquery capability?
Thanks!
You can attach to when the page loads using jQuery like this:
$(function() {
// Your code here.
});
If that's the only thing you'd be using jQuery for, though, I'd probably stick with your third option you listed there.
Javascript and jQuery code is generally best put right before the </body>
tag.
Use $(document).ready
$(document).ready(function() { code goes here; } );
Since you are using jQuery, you should use the facilities it provides..
$(document).ready(
function(){
/*You code goes here..*/
});
精彩评论