I notice this is the case mainly for rather sophisticated pages. However, why don't developers simply combine the JS files and condense it all together? Is it not a more efficient method, in terms of requests? I am not sure if it is but it would seem that way.
|
When talking about efficiency, as has been noted already, you've really got two issues.
It may perhaps be argued, but I think it fairly obvious that delivery is a higher priority. However when it comes to including scripts and dealing with changes in the site, having one massive 'god script' is a giant pain. Modularity is king of maintainability imho. One solution, like that mentioned by Ewan about Drupal, is to use your server-side language to dynamically 'compile' a JS file or two putting the whole mess together any time the code changes. Depending on how you achieve this it is possible to go ahead and use many sources during development and only one or a small number of them in production. There are also a few other practical matters to consider:
|
|||||
|
|
It's easier to develop a site if the scripts are separate but usually better to combine them for delivery as long as they aren't going to interact with each other. The reduction of requests will usually make the page load faster. Also, a combined script can usually be compressed to a smaller size than each individual file. The main reason files aren't combined is probably laziness but it's not actually too difficult to write a script to do it automatically. Some CMS systems, such as Drupal, have an option to send all the JavaScirpt in a single file. |
|||
|
|
|
It's easier not to. You take a bit of jQuery here and another useful script from there and just include both, and so it goes on. If you don't combine them it also makes upgrading one particular bit, like your jQuery library, easier. Also, if you're using some sort of CMS which uses lots of scripts then unless you want to modify the CMS and possibly break it, you put it up with so many scripts. Talking of jQuery, it's a good idea to use Google's content delivery network http://code.google.com/apis/libraries/devguide.html#jquery . However that means that if you have another script you're using multiple scripts again. |
|||
|
|