views:

10633

answers:

8

Google hosts some popular JavaScript libraries at: http://code.google.com/apis/ajaxlibs/

According to google:

The most powerful way to load the libraries is by using google.load() ...

What are the real advantages of using

google.load("jquery", "1.2.6")

vs.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"&gt;&lt;/script&gt;

?

+2  A: 

It lets Google change the URL (but they can't since the URL method is already established)

In theory, if you do several google.load()s, Google can bundle then into one file, but I don't think that is implemented.

James Curran
+2  A: 
  1. It allows you to dynamically load the libraries in your code, wherever you want.
  2. Because it lets you switch directly to a new version of the library in the javascript, without forcing you to rebuild/change templates all across your site.
d8uv
Second statement is wrong you do still need to specify library version.
Artem Barger
Specifying version "1" loads the latest revision available for version 1, e.g. google.load("jquery", "1");
Robert Claypool
+1  A: 

You might want to load a library only under special conditions.

Additionally the google.load method would speed up the initial page display. Otherwise the page rendering will freeze until the file has been loaded if you include script tags in your html code.

aemkei
+15  A: 

Aside from the benefit of Google being able to bundle multiple files together on the request, there is no perk to using google.load. In fact, if you know all libraries that you want to use (say just jQuery 1.2.6), you're possibly. making the user's browser perform one unneeded HTTP connection. Since the whole point of using Google's hosting is to reduce bandwidth consumption and response time, the best decision - if you're just using 1 library - is to call that library directly.

Also, if your site will be using any SSL certificates, you want to plan for this by calling the script via Google's HTTPS connection. There's no downside to calling a https script from an http page, but calling an http script from an https page will causing more obscure debugging problems than you would want to think about.

Eric Caron
I suggest avoiding https unless you have to... There is a slight performance hit for the client when they retrieve the HTTPS source vs. HTTP source... More importantly, HTTPS content isn't usually cached so you are missing out on the caching benefit aspect.
Dscoduc
However retrieving HTTP javascript for a HTTPS page defeats man-in-the-middle protection - Mallory can inject his malicious javascript into this HTTP request, and all your HTTPS security goes out the window.
bdonlan
The performance difference between HTTP and HTTPS is so small it's insignificant, and I really wouldn't worry too much about it.
Jonathan Prior
@Jonathan: *Other* than the fact the HTTP one can be cached, the HTTPS one can't. Which is significant, but if the page is already secure, probably a price worth paying.
T.J. Crowder
@bdonlan I'm pretty sure Mallory is a chick. I mean, as the story goes.
pferdefleisch
+3  A: 

I find it's very useful for testing different libraries and different methods, particularly if you're not used to them and want to see their differences side by side, without having to download them. It appears that one of the primary reason to do it, would be that it is asynchronous versus the synchronous script call. You also get some neat stuff that is directly included in the google loader, like client location. You can get their latitude and longitude from it. Not necessarily useful, but it may be helpful if you're planning to have targeted advertising or something of the like.

Not to mention that dynamic loading is always useful. Particularly to smooth out the initial site load. Keeping the initial "site load time" down to as little as possible is something every web designer is fighting an uphill battle on.

A: 

Personally, I'm interested in whether there's a caching benefit for browsers that will already have loaded that library as well. Seems like if someone browses to google and loads the right jQuery lib and then browses to my site and loads the right jQuery lib... ...both might well use the same cached jQuery. That's just a speculative possibility, though.

Edit: Yep, at very least when using the direct script tags to the location, the javascript library will be cached if someone has already called for the library from google (e.g. if it were included by another site somewhere).

Tchalvak
A: 

If you were to write a boatload of JavaScript that only used the library when a particular event happens, you could wait until the event happens to download the library, which avoids unnecessary HTTP requests for those who don't actually end up triggering the event. However, in the case of libraries like Prototype + Scriptaculous, which downloads over 300kb of JavaScript code, this isn't practical.

Andrew Noyes
A: 

Dave Ward wrote an indepth post about this entitled 3 reasons why you should let google host jquery for you:

http://encosia.com/2008/12/10/3-reasons-why-you-should-let-google-host-jquery-for-you/

broken down from the above post

  1. Decreased Latency
  2. Increased parallelism
  3. Better caching
Si Philp