Google CDN

20 Jan

I often find myself writing the following

<script type=”text/javascript” src=”/js/jQuery.min.js”></script>

and thinking, “this is silly, why don’t I fetch it from jQuery.com.” Without really having a good reason to choose either way I just kept on with what I knew, “if it aint broke…” right? Well today I came across this article and now I do have a reason to change my ways. Ok, ok, I’m a bit slow maybe (the article if from 2008) but better late than never.

<script type="text/javascript"
 src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
</script>
This is better for two reasons. 1) My server doesn't have to do any work to provide the file. 2) As more people use this method the chance that the file is cached increases, with a bit of luck jQuery will already be available on the client.

A follow up article on the same site makes it clear that there are are a few gotchas and explains how to handle them. Particularly the effect of using http: links in https: pages. Turns out that if you exclude the protocol specification in the link URL then the browser’s underlying protocol will be used instead.

<script type="text/javascript"
 src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
</script>

Then I started to wonder what other javascript resources were lying about Google’s CDN. Here are Google’s own contributions and here are some third party libraries like jQuery. Take a look at Google’s visualization library, I am particularly impressed by the suggestion that Google docs spreadsheet can be used as input for a visualization! Bit of a shame that it embeds a flash object thought :/

Stuart McFarlane

Mobile Media Constructs