Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

When talking about efficiency, as has been noted already, you've really got two issues.

  • Efficiency in the development process
  • Efficiency in delivering the site to the end user

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:

  • You can offload some of your bandwidth for scripts that are hosted by services such as Google Libraries API or Yahoo's YUI. Increasingly, users have these files already onhand from other sites and, if nothing else, loading your site from more connections will speed up delivery.
  • There are cases where you don't need the same functionality on every page and want to easily leave out some scripts while keeping the ones relevant to the current page.
  • Every now and then you may actually want to load individual scripts after load-time using AJAX and whatnot based on the user's browsing context.
share|improve this answer
1  
And you can use something like Ant to compress and minify all the scripts into one. Html5Boilerplate handles this for you. –  Blowski May 6 '11 at 15:50

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.