ColdFusioning.com
Pronunciation \kold-fy-zhn-ing\ Noun: The actions of one that writes ColdFusion.

Please check out my new site: http://ja.mesbrown.com/

Load jQuery From Google Without Risk of Breaking Your Site

We've all heard the best practices recommendation to load jQuery (or other JavaScript libraries) from a CDN, such as Google's for performance reasons. But what happens if, in the rare chance, Google's network is down or inaccessible? At a minimum, your site would start throwing errors & wouldn't function properly.

Fear not, all is not lost. Using the code below, we can try to load the file from the Google CDN and then quickly test if it successfully loaded. If it did load properly, we don't do anything different. But if it didn't, we can load the file from another location, assumingly our web server. Then our site will function properly and once Google's CDN is re-accessible, visitors will continue to benefit from the use of the CDN.

view plain print about
1<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
2
3<script type="text/javascript">
4if (typeof jQuery === 'undefined') document.write("<scr"+"ipt src='/js/jquery-1.4.2.min.js'></scr"+"ipt>");
5</script>


TweetBacks

Comments

Very useful. Thanks for posting

# Posted By Zeb | 7/14/10 8:07 PM

Useful. I think

> if (typeof jQuery === 'undefined') document.write("<scr"+"ipt src='/js/jquery-1.4.2.min.js'></scr"+"ipt>

is having extra string concats for <script> tags like </scr"+"ipt>

# Posted By ManiKanta G | 7/26/11 10:53 AM

Recent Comments

RSS

Subscribe