开发者

way to load javascript code in web page

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?

  1. put body onload?
  2. startu开发者_如何学Gop code in body of .js file (not in function)?
  3. attach to window load event?
  4. 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..*/


});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜