X

LATEST #JQUERY NEWS

Quick Tip: How to Load jQuery Conditionally


Many developers like to link to an externally hosted jQuery library, like for example Google’s jQuery library (http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js), but the problem with linking to an external jQuery library is that they can be a bit unreliable. If for some reason there are any issues with the server, the script may not be able to be accessed and your jQuery code won’t work.

HTML5’s boilerplate¬†uses a really cool conditional statement that will load a local jQuery library on the occasion that, for whatever reason, your external library isn’t loading or working correctly. Check it out:

<!-- Grab Google CDN jQuery. fall back to local if necessary -->
<script>!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"><\/script>')</script>

Of course, you’ll need to insert your own path to wherever your local jQuery files are located (instead of ‘<script src=”js/jquery-1.4.2.min.js”></script>’), but this is a very simple way to make sure that your jQuery library always loads.

Recently Published

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of¬†checkbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...