Why You Should Get Your JS from Google

26 Nov 2009 | By Ric Roberts | Tags performance google

If you’re using popular JavaScript libraries such as jQuery, Prototype, Scripty, or MooTools, and you’re not taking advantage of Google’s Ajax Libraries content delivery service, then you might be missing a trick.

By using Google’s Ajax Libraries, you get some of the benefits of a CDN — lower latency requests no matter where your users are.

You also get better caching: it’s probable your users will already have the JavaScript file in their browser cache from another site and so won’t have to re-download it.

You can load a library by using Google’s AJAX loader. If you provide a partial version number, Google will dish up the latest version that matches the pattern. The example below would load v1.3.2 if called today (but so would using “1.3”). This is useful if you’re relying on features introduced by a certain version.

<script src="http://www.google.com/jsapi"></script>
<script>
  // Load jQuery
  google.load("jquery", "1");
</script>

Or, you can just reference a particular version’s file directly (which will result in fewer requests).

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
blog comments powered by Disqus