Snapshot of a jQuery book
One day in the train with a jQuery book.

Optimising the size of your JavaScript

Few simple rules for optimising JavaScript file size:

  • Combine selected libraries and their plugins in to a one file
  • Minify that file
  • Compress the minified file with Gzip
  • Check with your web site session initialisation if the visitors browser supports Gzip
  • Offer either of the files depending of the visitors capabilities.

The server side checking of browsers support for Gzip could be done with the following PHP code:

$_SESSION['gzip'] = false;

if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {

    if ([strpos](http://www.php.net/strpos)($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {

        $_SESSION['gzip'] = true;

    }

}

Similarly you could compress CSS files too. For this you could try the YUI compressor.