Fallback on Local jQuery If CDN Fails


Many big websites are now offering CDN services for things like jQuery. Google offers a few of the biggest Javascript libraries from their own CDN network. Microsoft also allows you to use a CDN to implement jQuery in your website.

The benefit of this is that the loading of the jQuery will reduce bandwidth because it will be downloaded from a local server, which therefore should download faster and this script should be cached by the many different websites using it.

Using a CDN to delivery your CSS and Javascript will speed up your page loading times. But many people don't always like to use this solution because what if something does happen to these big companies CDN? Your jQuery will not load and some of your website features will no longer work.

Therefore you need a way to load a local jQuery file if for some reason your CDN jQuery fails to load.

Use the following snippet you can download jQuery from the Google CDN if it fails then you will use a local version.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-2.1.0.min.js"></script>')</script>
Back to top ^

Join Paulund

  • Get access to 100s of premium tutorials and downloadable content
  • Members content consists of Premium WordPress plugins, CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
  • In-depth development tutorials
  • Priority tutorial requests
  • Priority email development support
  • No ads

Here at Paulund you'll find Web Development tutorials and snippets focusing mainly on PHP, WordPress and CSS. Members will get access to premium tutorials that go into more detail about development with these languages. You will also get access to downloadable premium content based on the tutorial.

Join Paulund

Leave a Reply

Your email address will not be published. Required fields are marked *