Letting Google Load jQuery… The Right Way
Posted: December 7th, 2009 | Filed under: jQuery | 4 Comments »For some time now I have been letting google load the jQuery and jQuery UI libraries for me. There are many benefits to it which I will not get into, you can read more reasons to use it elsewhere. However I did notice something worth posting.
You may be loading the jQuery library like so:
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" type="text/javascript"></script>
Well if you are then you are doing it wrong.
It was ySlow that told me why. Try changing that one line to the following and take another look at ySlow.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
Notice the difference? Google is now gzipping the library when its called from googleapis.com instead of googlecode.com thus lowering the files size drastically.
For those of us that are to lazy to try it out here is the result with the googleapis.com loaded instead of the googlecode.com.
You can see instead of downloading 57.2kb that you would with the googlecode file the gzip compression brings it down to 19kb.
The same rule would apply for other libraries that are hosted by google. such as mootools, or even the jQuery UI.
So next time you go to include the library make sure to do it the right way.
Now I just need to switch all the sites I have that are using it.. such as this one.
If you enjoyed this post, make sure you subscribe to my RSS feed!



[...] This post was mentioned on Twitter by 3L1j4h m4nO|2 and morena84, CoryMathews. CoryMathews said: Letting Google Load #jQuery… The Right Way http://bit.ly/7XmoH8 (by @CoryMathews) [...]
Hi Cory,
Would you recommend using this method over using Googles AJAX API functionality like so:
// Load jQuery
google.load(“jquery”, “1.3.2″);
@James
I am not sure how it compares, Without testing I would guess that directly loading the jquery file should be faster. Using the form you mention requires loading some google js that would then process and load the jquery. Seems like more work then just directly calling the jquery. It bet it would also delay the running of the jquery since it would not start loading until the js was run. But it may be worth a test here in the near future.
Directly loading the jQuery library may be faster in terms of the request speed, but if you have many requests to make in order to load your page then it would be faster to load from another domain. Some browsers limit how many requests can simultaneously be made per domain, and this increases the amount of requests that can be made.
I would always recommend using a content delivery network when possible.