Fallback on Local jQuery If CDN Fails
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.
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>
Learn how to code with Treehouse
- Learn projects with access to 1000+ videos
- Practice live with our Code Challenge Engine
- Get help in our members-only forums
Join the weekly newsletter
Sign up for our newsletter. You'll be updated on new tutorials as they come available.
Subscribe today and get a 50% discount code to Paulund