开发者

Enclosing external jQuery file in $(document).ready()

开发者 https://www.devze.com 2023-03-01 19:21 出处:网络
I have external JavaScript files that contain only jQuery.S开发者_JAVA技巧hould I enclose all of the code in each of these external files in $(document).ready()? What is the best practice here?

I have external JavaScript files that contain only jQuery. S开发者_JAVA技巧hould I enclose all of the code in each of these external files in $(document).ready()? What is the best practice here?

Thanks!


Should I enclose all of the code in each of these external files in $(document).ready()?

Only if they need to wait for the DOM to be complete before they run.

What is the best practice here?

Combining them into one file, minifying it and serving it with appropriate HTTP compression and cache control it.


Include the jquery script above them in your page, and have each of the other scripts included after it, in the normal way. But inside each of those scripts, put all the necessary code in the $(document).ready() structure. You can use it multiple times; you aren't restricted to just once.

0

精彩评论

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