开发者

Organizing script files

开发者 https://www.devze.com 2023-01-27 21:30 出处:网络
I usually have jQuery code that is page specific along with a handful of functions that many pages share. One approach is to make seperate files for organizing, but i\'m thinking that putting all the

I usually have jQuery code that is page specific along with a handful of functions that many pages share. One approach is to make seperate files for organizing, but i'm thinking that putting all the script in one file and making comments in the file for readability would also work. Then when the site goes live I can开发者_C百科 minify and obfuscate if needed.

I think the question comes down to limiting http requests or limiting file size. Is one of these a bad habit?


You can have it both ways. Develop with as many individual .js files as you need. Then use a build/deployment process that assembles the files into one larger one, then pushes them through something like Google's Closure Compiler. Compression can be handled transparently by your web server if configured properly.

Of course, this implies a structured development and deployment workflow -- e.g., with files to be assembled/compiled in a specific directory, separated from files that should be served as-is.

References:

  • Closure Compiler
  • Apache Ant
  • Automating the Closure Compiler with Ant


If you can put all the scripts in one file which is minified then that's what you should do first.

Also if your webserver sends out gzipped content the actual script transfer would be small, and the script will be cached on client. Since tcp transfers starts out slow and increase in speed, limiting the number of requests is the best way to speed up the overall loading of a page.

This is the same reason you see sites concatenating images into one larger image, and using CSS to display the correct part of it.

0

精彩评论

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