38

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"></script>

?

Vilmantas Baranauskas
  • 6,596
  • 3
  • 38
  • 50

7 Answers7

31

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.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Eric Caron
  • 6,181
  • 1
  • 24
  • 26
  • 7
    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 Feb 13 '09 at 20:38
  • 1
    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 May 05 '09 at 02:41
  • 3
    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 May 14 '09 at 18:45
  • 3
    @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 May 23 '10 at 14:38
  • 2
    @bdonlan I'm pretty sure Mallory is a chick. I mean, as the story goes. – mraaroncruz Oct 19 '10 at 13:13
  • 1
    @Eric Caron: I just found this old discussion via a search, and the answer given is ... I don't mean to be harsh, but it's wrong. That said, you're partially right, if you only load 1 or maybe 2 libraries from Google's CDN, then loading them directly via SCRIPT SRC= is best. The benefit of using Google's loader is that the subsequent script loads happen *asynchronously*, i.e. not blocking browser rendering. In other words, loading the JSAPI blocks rendering, but the following google.load("jquery") etc complete without blocking browser rendering. Alternatives are LABjs , RequireJS , HeadJS etc. –  Jan 05 '11 at 19:36
4
  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
  • 656
  • 7
  • 11
  • Second statement is wrong you do still need to specify library version. – Artem Barger May 14 '09 at 18:56
  • 4
    Specifying version "1" loads the latest revision available for version 1, e.g. google.load("jquery", "1"); – Robert Claypool Nov 23 '09 at 04:57
  • 2
    Robert: You don't need to use google.load() to automatically switch to new versions... http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js will always load the latest update of version 1. – Simon East Feb 04 '11 at 08:38
  • 1
    @Simon While it is true you can use the /1/ link for jQuery, that is a very bad idea in a production environment. One, it has a very short-lived cache as compared to the specific versioned urls, and two, you have no control over when it changes, and any breaking changes could break your site without your knowledge. – LocalPCGuy Mar 07 '13 at 14:14
  • @LocalPCGuy - yes, totally agree, I never use it on production sites. But if for some reason someone wants to stay on the latest version during development, it is possible *without needing `google.load()`*. – Simon East Mar 07 '13 at 23:51
3

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
  • 101,701
  • 37
  • 181
  • 258
3

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.

user50297
  • 31
  • 2
1

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
  • 11,076
  • 8
  • 37
  • 29
0

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).

Kzqai
  • 22,588
  • 25
  • 105
  • 137
0

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
  • 5,248
  • 1
  • 18
  • 14