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

前端 未结 23 1453
太阳男子
太阳男子 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:50

    You can use code like:

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>window.jQuery || document.write('<script type="text/javascript" src="./scripts/jquery.min.js">\x3C/script>')</script>
    

    But also there are libraries you can use to setup several possible fallbacks for your scripts and optimize the loading process:

    • basket.js
    • RequireJS
    • yepnope

    Examples:

    basket.js I think the best variant for now. Will cach your script in the localStorage, that will speed up next loadings. The simplest call:

    basket.require({ url: '/path/to/jquery.js' });
    

    This will return a promise and you can do next call on error, or load dependencies on success:

    basket
        .require({ url: '/path/to/jquery.js' })
        .then(function () {
            // Success
        }, function (error) {
            // There was an error fetching the script
            // Try to load jquery from the next cdn
        });
    

    RequireJS

    requirejs.config({
        enforceDefine: true,
        paths: {
            jquery: [
                '//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min',
                //If the CDN location fails, load from this location
                'js/jquery-2.0.0.min'
            ]
        }
    });
    
    //Later
    require(['jquery'], function ($) {
    });
    

    yepnope

    yepnope([{
      load: 'http://ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min.js',
      complete: function () {
        if (!window.jQuery) {
          yepnope('js/jquery-2.0.0.min.js');
        }
      }
    }]);
    
    0 讨论(0)
  • 2020-11-21 07:52

    You can achieve it like this:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
    
    <script>
           window.jQuery || document.write('<script src="/path/to/your/jquery"><\/script>');
    </script>
    

    This should be in your page's <head> and any jQuery ready event handlers should be in the <body> to avoid errors (although it's not fool-proof!).

    One more reason to not use Google-hosted jQuery is that in some countries, Google's domain name is banned.

    0 讨论(0)
  • 2020-11-21 07:52

    The inability to load the resource from an external data store beyond your control is difficult. Looking for missing functions is totally fallacious as a means to avoid suffering a timeout, as described herein: http://www.tech-101.com/support/topic/4499-issues-using-a-cdn/

    0 讨论(0)
  • 2020-11-21 07:53

    If you have modernizr.js embedded on your site, you can use the built-in yepnope.js to load your scripts asynchronously - among others jQuery (with fallback).

    Modernizr.load([{
        load : '//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'
    },{
        test : window.jQuery,
        nope : 'path/to/local/jquery-1.7.2.min.js',
        both : ['myscript.js', 'another-script.js'],
        complete : function () {
            MyApp.init();
        }
    }]);
    

    This loads jQuery from the Google-cdn. Afterwards it's checked, if jQuery was loaded successfully. If not ("nope"), the local version is loaded. Also your personal scripts are loaded - the "both" indicates, that the load-process is iniated independently from the result of the test.

    When all load-processes are complete, a function is executed, in the case 'MyApp.init'.

    I personally prefer this way of asynchronous script loading. And as I rely on the feature-tests provided by modernizr when building a site, I have it embedded on the site anyway. So there's actually no overhead.

    0 讨论(0)
  • 2020-11-21 07:56

    Here is a great explaination on this!

    Also implements loading delays and timeouts!

    http://happyworm.com/blog/2010/01/28/a-simple-and-robust-cdn-failover-for-jquery-14-in-one-line/

    0 讨论(0)
  • 2020-11-21 07:57

    The easiest and cleanest way to do this by far:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="path/to/your/jquery"><\/script>')</script>
    
    0 讨论(0)
提交回复
热议问题