Tuesday, 19 June 2012

jQuery: what if Google Libraries API is offline?


When we build pages with jQuery, we have two possible way of including the library.
First we can actually save the .js file in a specific folder of our web site and then include it directly.
Another way is to use the Google Libraries API deposit and get it from there.

But what if the GLA is not available? In those cases some functionality might be lost.
Here I show you how to create a proper fallback.

The code

The code is straight and simple.
First we try to load the jQuery library from the GLA.
If it fails, we load the local copy of it.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="jsLocalPath/jquery.min.js"<\/script>')</script>
There's not much to say about the above snippet, as you can see. The only thing you should check and change is the jsLocalPath, according to your js libraries folder path.

As usual, please share your experience using the comments section below.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.