JavaScript
Article

The Best Way to Load jQuery Library (with fallback)

By Sam Deering

Here is what I think is the best and most reliable way to load jQuery library and jQuery UI Libraries. Nowadays everyone is using the Google CDN to load in the latest version of the jQuery library, this is leading to many browsers caching this file. If your web pages also do this it is very likely that the viewer already has this file cached and hence it will be one less file for them to load before getting to view your page.

On the off chance that the Google CDN is down (not likely) OR most likely you are working from a localhost for development there is a fall back to load a local version of jQuery Library.

Load jQuery Library



Load jQuery Library



Other Methods

Dynamically Load jQuery Library Using Plain JavaScript

  • http://www.echteinfach.tv/ Mathematik Videos

    thanks for the tip! I always asked myself what happens if the CDN is down. This code avoids one of the disadvantages that could appear!

  • Mathias Philippe

    It’s what they do in html5 Bolierplate if I remember, I also do it this way now, very useful.

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.