Revised jQuery with multiple CDN Fall backs with local as a fail safe.

So a while back I posted my solution of jQuery with an alternative CDN fall back with the local as a fail safe, it turns out there was a flaw with my code. While I was using YSlow I noticed that when I included a broken link to Google’s hosted jQuery instead of just downloading Microsoft’s jQuery it downloaded Microsoft’s jQuery & the local hosted jQuery. So I’ve made some adjustments to my code and now it works. I also changed the order and included an extra CDN hosted jQuery - Now it might be a bit excessive but oh well. The order now goes as follows - Google CDN > jQuery CDN > Microsoft CDN > then Local as the final fall back. Why jQuery CDN before Microsoft? It had a faster response time when I was testing my updated solution.

Updated Code Below:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script>
//If Google CDN Network is down, Use the same one that jQuery hosts
window.jQuery || document.write('<script src="//code.jquery.com/jquery-1.6.min.js">\x3C/script>')</script>
<script>
//If jQuery CDN is down, use Microsoft's CDN Network
window.jQuery || document.write('<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.min.js">\x3C/script>')</script>
<script>
//If all else fails, use local
window.jQuery || document.write('<script src="public/javascripts/jquery-1.6.min.js">\x3C/script>')</script>

Edit: So if you decide to use the code above I would like to point out that if you are testing your html file on your local machine if you leave out the fail-safe local jQuery it won’t work because protocol-less url will clash with the file:/// protocol thus it won’t download it. I never really noticed it until I was helping a colleague (which to kill some time I decided to omit the final check of jQuery which used the locally hosted jQuery) because I always had the fail safe jQuery in my sites. Also helps if I would of read the comments in the blog post which first showed me about the protocol-less url of jQuery. Live and learn…

Tags: jquery web