Blend multiple jQuery files - Best practice?
I'm starting to learn JavaScript and jQuery. I managed to do some tweakings and stuff, everything's fine so far. But I was thinking if it is a best practice to mix multiple jQuery files and plugins. For example:
I have jQuery 1.3.2 min.js, jQuery easing 1.3.js, jQuery Cycle min.js and jQuery UI. Would it be recommended to put together jQuery (main), jQuery easing and jQuery cycle in just one file?
Please give me som开发者_如何学Goe light here.
Thanks,
Keep the files separate and use your back-end to minify and gzip them as part of your deploy process. As others have mentioned this will save your users making unnecessary requests and it will save you having to maintain a huge ugly file.
The added benefit here is that you can actually keep the verbose, commented, full files for all of these in your source tree (for reading/etc) while serving the compressed ones to users.
You can do so. It saves the amount of HTTP requests to haul all the JS code in. It is only a bit harder to maintain, but in my opinion it doesn't outweigh the benefits you (and the client!) have with less HTTP requests.
If you're using a server side language and good deployment tools, you can even automate the minifying and merging of JS files before publishing. We also do it here with a little help of YUI Compressor and a few lines of Java code in a batch application. Works great, the endusers have the benefit of less HTTP requests and the developers have the benefit of not doing all that merging and maintainence manually.
Given that Google is thinking about considering site response speed as a qualifier for rank you may be better off compiling all JS into one file. YSlow is a good tool for helping you determine the most optimal way to do this sort of thing.
No, I would keep the files separate. It keeps the functionality separate, and makes it easier to re-plugin any later versions of the plugins should you want.
Also, by editing the file to include all plugin code, you are inherently introducing risk, as the file itself will not have been subject to the testing of the original code base authors.
Certainly, I would argue any benefits are considerably outweighed by the pitfalls.
This is probably a late answer, but I would try to serve as many files as I could from the Google, Microsoft, etc CDN's. Even if you reference multiple files, the chances that someone already downloaded file from some other site is high and they will avoid downloading it again.
As for custom, in-house, JavaScript, I write 99% of my JS in a jQuery plugins, even if I am only going to use that plugin on one form. Then I can combine all my JS into one file, run it through Google Closure, and have it ready to go.
The other 1% is where each page invokes the plugins and passes whatever params it needs.
精彩评论