Posted By

stancox on 07/11/10


javascript library google load

Versions (?)

Who likes this?

3 people have marked this snippet as a favorite


Load jQuery From Google Without Risk of Breaking Your Site

 / Published in: jQuery


Related Categories: jQuery, Tips

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.

  1. <html>
  3. <head>
  5. <script type="text/javascript" src=""></script>
  8. <script type="text/javascript">
  9. if (typeof jQuery === 'undefined') document.write("<scr"+"ipt src='jquery-1.3.2.min.js'></scr"+"ipt>");
  10. </script>
  11. </head>
  13. <body>
  14. <script type="text/javascript">
  15. $(document).ready(function(){
  16. $('#test_id').append('test test test');
  17. })
  18. </script>
  19. </body>
  20. <span id="test_id">
  21. </span>
  22. </html>

Report this snippet  

You need to login to post a comment.