Logo

WebMuch

Everything about the web

  • Home
  • Tutorials
  • Articles
  • Resources
  • About
  • Contact

Subscribe To: RSS Feed RSS | Email

How & why you should use Google CDN

jQuery Google CDN ImageIn this article I will share the importance of using a Google CDN link of our favorite JavaScript library and how to implement it with two different methods.

By: Aayush

Date: May 30, 2009

Tags: Google, JavaScript, jQuery

Author: Aayush Shastri

Hello! I'm a freelance web and graphic designer who loves working on anything related to web. I'm currently the website administrator of WebMuch. You can find me on Twitter! and Facebook.

What is Google’s CDN?

Google CDN stands for Google Content Distribution Network. It provides a loading architecture for the most popular, open source JavaScript libraries.

Available JavaScript Libraries Image

Why should I use Google’s CDN links?

Most of the websites I see, still host their JavaScript Library on their own server (Most, not all). Actually never have I ever seen any Web Development Blog hosting the library on their own server. They always use Google CDN, which is great because it has a number of advantages:

  1. Google’s Content Distribution Network has libraries on their various servers across the world and if you use their CDN link, when a user’s browser resolves the URL, their browser will automatically download the file from the closest available server, which will be a much faster download than if you force a user to download the file from your host server in whichever country your website is hosted.
  2. Google’s Content Distribution Network will save you a great deal of bandwidth if you have large traffic on your website and a limited bandwidth with your host.
  3. There is a good chance that the Google hosted version of that library is already cached on the user’s browser cache, as lot of big websites and portals use Google’s CDN links including Google itself and Google’s servers instruct browsers to cache the file for one year if there are quite a few requests for the same hosted file.

How to use Google CDN?

Google’s CDN provides most of the popular, open source JavaScript Libraries with two ways of using each of them via the direct path in the <script/> tags and via The Google AJAX Libraries API.

To use the Google AJAX Libraries API we need to include the Google “jsapi” script:

<script src="http://www.google.com/jsapi"></script>

The Google AJAX Libraries API provides a simple and powerful google.load() method which accepts two arguments, the first argument to google.load is the name of a library. The second argument is the version of the library:

<script>
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.1");
    google.setOnLoadCallback(function(){
        // use this instead of:
        /*
            $(document).ready(function(){  });
        */
    });
</script>

But to use this API, there is a different approach for the ready functions of all the libraries. For example, in the above code, we use google.setOnLoadCallback(function(){ }); instead of jQuery’s ready function. In-fact we use google.setOnLoadCallback(function(){ }); instead of all different library’s ready functions. This can be seen as a drawback or a good feature; I see it as a good feature as it gives me a single ready function for all libraries.

The second method to use Google’s CDN links is by the direct paths of the libraries in the <script/> tags

Below is a list of all the JavaScript Libraries Google CDN provides with the direct paths and google.load implementation:

jQuery

name: jquery
versions: 1.2.3, 1.2.6, 1.3.0, 1.3.1, 1.3.2
load request: google.load("jquery", "1.3.2");
extras: uncompressed:true, e.g., google.load("jquery", "1.3.2", {uncompressed:true});
path: http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
path(u): http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js

jQuery UI

name: jqueryui
versions: 1.5.2, 1.5.3, 1.6, 1.7.0, 1.7.1
load request: google.load("jqueryui", "1.7.1");
extras: uncompressed:true, e.g., google.load("jqueryui", "1.7.1", {uncompressed:true});
path: http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js
path(u): http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.js
note: This library depends on jquery. Before loading this module, you must load jquery. e.g.:
        google.load("jquery", "1.3.2");
        google.load("jqueryui", "1.7.1");

Prototype

name: prototype
versions: 1.6.0.2, 1.6.0.3
load request: google.load("prototype", "1.6.0.3");
path: http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js

script.aculo.us

name: scriptaculous
versions: 1.8.1, 1.8.2
load request: google.load("scriptaculous", "1.8.2");
path: http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.2/scriptaculous.js
note: This library depends on Prototype. Before loading this module, you must load Prototype. e.g.:
        google.load("prototype", "1.6");
        google.load("scriptaculous", "1.8.2");

MooTools

name: mootools
versions: 1.11, 1.2.1, 1.2.2
load request: google.load("mootools", "1.2.2");
extras: uncompressed:true, e.g., google.load("mootools", "1.2.2", {uncompressed:true});
path: http://ajax.googleapis.com/ajax/libs/mootools/1.2.2/mootools-yui-compressed.js
path(u): http://ajax.googleapis.com/ajax/libs/mootools/1.2.2/mootools.js

Dojo

name: dojo
versions: 1.1.1, 1.2.0, 1.2.3, 1.3.0, 1.3.1
load request: google.load("dojo", "1.3.1");
extras: uncompressed:true, e.g., google.load("dojo", "1.3.1", {uncompressed:true});
path: http://ajax.googleapis.com/ajax/libs/dojo/1.3.1/dojo/dojo.xd.js
path(u): http://ajax.googleapis.com/ajax/libs/dojo/1.3.1/dojo/dojo.xd.js.uncompressed.js

SWFObject

name: swfobject
versions: 2.1
load request: google.load("swfobject", "2.1");
extras: uncompressed:true, e.g., google.load("swfobject", "2.1", {uncompressed:true});
path: http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject.js
path(u): http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject_src.js

Yahoo! User Interface Library (YUI)

name: yui
versions: 2.6.0, 2.7.0
load request: load request: google.load("yui", "2.7.0");
extras: uncompressed:true, e.g., google.load("yui", "2.7.0", {uncompressed:true});
path: http://ajax.googleapis.com/ajax/libs/yui/2.7.0/build/yuiloader/yuiloader-min.js
path(u): http://ajax.googleapis.com/ajax/libs/yui/2.7.0/build/yuiloader/yuiloader.js

Source: Google

Thanks for reading.

Bookmark and Share
Like It?
Delicious Stumble Mixx Design Float
Visit Aayush Shastri's Website
 

Comments:

  1. L

    June 4, 2009

    looks like you took my advice and made the; thanks – it’s much more readable.

    you should turn off the comments if you delete them all anyways.

  2. L

    June 4, 2009

    > looks like you took my advice and made the

    i meant “made the corrections”

    your grammar is rubbing off on me :)

  3. Aayush

    June 4, 2009

    It was a comma or two and I didn’t approve your previous comment because it was extremely disrespectful.

  4. jet

    September 20, 2009

    Hello, great article.

    I have a question. What about GUnload() function? Since I read about the new google ajax API, I don’t know if it’s still convenient to use it.

    I’m starting a new proyect using google maps api, and I’d like to know the best way to work with this api.

    After spending lot of time collecting information, these are the best tips I’ve found:
    - use google ajax api
    - use jquery
    - load all the javascript libraries at the bottom of the page, before

    And this an example of my loader script:

    google.load(”maps”, “2″, {
    “language”: “es”,
    “other_params”: “sensor=false”
    });
    google.load(”jquery”, “1.3.2″);

    google.setOnLoadCallback(function(){
    if (GBrowserIsCompatible()) {
    var map = new GMap2($(”#map”).get(0));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    }
    });

    $(document.body).unload(function(){
    if (GBrowserIsCompatible()) {
    GUnload();
    }
    });

    What do you think?

    Thank you.

  5. Andrzej Michalski

    January 25, 2010

    We’re wanting to make use of CDN’s in general, but does anyone have any advice on using https – will browsers moan that content is coming from different sources? Does the Google CDN support being accessed via https?
    Andrzej

  6. Simon

    February 11, 2010

    The author states he has never seen a web development blog host it on their own server, perhaps he should take a look at a source for this page…

  7. Aayush

    February 16, 2010

    hah I know what it looks like, but if you see closely, I haven’t used actually used jQuery anywhere, as you can see I haven’t really been active on this blog. If I actually had a jQ component I would switch to the CDN.

Comments currently closed.

Advertise Here Advertise Here
User Link Feed
  • 15 Essentials of a Successful Website with Examples

    When hundreds of web sites are designed up every other day, your website need to have something special to attract your potential customers.

  • 50+ Online Design Magazines

    Design magazines have always been a great source of inspiration for designers. It brings latest trends and new ideas into the mind of designers.

  • 40 Examples of Cool Javascript

    JavaScript has blossomed so rightly. Since then, there are jquery, Mootools , Scriptallicious, Prototype etc..which really produces cool effects. In todays time, java script is a must have component.

  • Invoicera Unveils its New Invoicing Application for Designers & Freelancers

    Invoicera has recently unveiled the release of their new fully blown web application with tons of powerful features to further simplify the management of bills and invoices. The new version is a complete makeover and you may probably even fail to recognize it.

  • Apple iPad- A New Way to Feel the Design World

    After literally years of speculation, Apple’s tablet is here and it’s called the iPad. iPad runs almost 140,000 apps from the App Store. It’s also a useful tool for designers, illustrators and digital artists looking to get creative on the move.

  • Top 60 Outstanding Photoshop Tutorials

    Again it’s time for photoshop tutorials. The tutorials are good as an inspiration for creative ideas. You have to learn how to combine photos and add special effects to turn a normal photograph into a stunning artwork.

  • 20+ Easy to Work Content Management Systems

    Choosing a CMS for your project depends upon the requirement and complexity of the project.

  • 40 + iPhone Apps for Designers and Developers

    The iPhone is more than just a phone. It combines three devices in one: a revolutionary phone, a wide screen iPod and a groundbreaking Internet device.

  • 14 Best CSS Editors for Web Designers & Developers

    CSS definitely provides you the ease of changing the appearance of your entire website by just modifying a single file. What makes the entire process of changing the CSS files more easy is the use of CSS Editors.BestDesignTuts.com has brought to you 14 such CSS Editors which will cut half of the work load of every designer.

  • 404! With a Difference…

    404! Error Pages once used to be seen as the most unwanted page that any visitor would like to see, and therefore the designers did not give the required piece of attention to it.

SubcribeSubmit a Link
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
  • Recent Posts

    • Top 10 Must Follow Web Blogs + 6 Web News Repositories
    • Image Flip Using jQuery
    • New Author + Resources Category on WebMuch!
    • Animated Navigation Bar Using jQuery
    • How & why you should use Google CDN
  • Categories

    • Articles
    • HTML & CSS
    • JavaScript & AJAX
    • News
    • PHP
    • Round-ups
    • Tutorials
WebMuch Home
© Copyrights 2009

Other Projects:

PicMuch