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

前端 未结 23 1838
太阳男子
太阳男子 2020-11-21 07:19

What would be a good way to attempt to load the hosted jQuery at Google (or other Google hosted libs), but load my copy of jQuery if the Google attempt fails?

I\'m n

23条回答
  •  轮回少年
    2020-11-21 07:46

    if (typeof jQuery == 'undefined') {
    // or if ( ! window.jQuery)
    // or if ( ! 'jQuery' in window)
    // or if ( ! window.hasOwnProperty('jQuery'))    
    
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = '/libs/jquery.js';
    
      var scriptHook = document.getElementsByTagName('script')[0];
      scriptHook.parentNode.insertBefore(script, scriptHook);
    
    }
    

    After you attempt to include Google's copy from the CDN.

    In HTML5, you don't need to set the type attribute.

    You can also use...

    window.jQuery || document.write('
    
                                     
                  
提交回复
热议问题