Search code examples
jqueryprototypejsgoogle-cdn

Google CDN for jquery or prototypejs or any other if not loaded


I am using google CDN for jQuery and prototypeJS library in my pages. For any reason, if those library are not loaded what measure can I take so that my pages function properly.

EDIT

I have found this link in SO

Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail

which is very similar to my question.

Sorry to ask the same thing which has been asked already.


Solution

  • if (typeof jQuery == 'undefined')) {
      // Insert your local copy
    
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = 'to/local/copy/jquery.js';
    
      var scriptHook = document.getElementsByTagName('script')[0];
      scriptHook.parentNode.insertBefore(script, scriptHook);
    
    }