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

前端 未结 23 1407
太阳男子
太阳男子 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 08:04

    I made a Gist that should dynamically load jQuery if it isn't already loaded, and if the source fails, it proceeds onto fallbacks (stitched together from many answers): https://gist.github.com/tigerhawkvok/9673154

    Please note I plan to keep the Gist updated but not this answer, for what it's worth!

    /* See https://gist.github.com/tigerhawkvok/9673154 for the latest version */
    function cascadeJQLoad(i) { // Use alternate CDNs where appropriate to load jQuery
        if (typeof(i) != "number") i = 0;
        // the actual paths to your jQuery CDNs
        var jq_paths = [
            "ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js",
            "ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.0.min.js"
        ];
        // Paths to your libraries that require jQuery
        var dependent_libraries = [
            "js/c.js"
        ];
        if (window.jQuery === undefined && i < jq_paths.length) {
            i++;
            loadJQ(jq_paths[i], i, dependent_libraries);
        }
        if (window.jQuery === undefined && i == jq_paths.length) {
            // jQuery failed to load
            // Insert your handler here
        }
    }
    
    /***
     * You shouldn't have to modify anything below here
     ***/
    
    function loadJQ(jq_path, i, libs) { //load jQuery if it isn't already
        if (typeof(jq_path) == "undefined") return false;
        if (typeof(i) != "number") i = 1;
        var loadNextJQ = function() {
            var src = 'https:' == location.protocol ? 'https' : 'http';
            var script_url = src + '://' + jq_path;
            loadJS(script_url, function() {
                if (window.jQuery === undefined) cascadeJQLoad(i);
            });
        }
        window.onload = function() {
            if (window.jQuery === undefined) loadNextJQ();
            else {
                // Load libraries that rely on jQuery
                if (typeof(libs) == "object") {
                    $.each(libs, function() {
                        loadJS(this.toString());
                    });
                }
            }
        }
        if (i > 0) loadNextJQ();
    }
    
    function loadJS(src, callback) {
        var s = document.createElement('script');
        s.src = src;
        s.async = true;
        s.onreadystatechange = s.onload = function() {
            var state = s.readyState;
            try {
                if (!callback.done && (!state || /loaded|complete/.test(state))) {
                    callback.done = true;
                    callback();
                }
            } catch (e) {
                // do nothing, no callback function passed
            }
        };
        s.onerror = function() {
            try {
                if (!callback.done) {
                    callback.done = true;
                    callback();
                }
            } catch (e) {
                // do nothing, no callback function passed
            }
        }
        document.getElementsByTagName('head')[0].appendChild(s);
    }
    
    /*
     * The part that actually calls above
     */
    
    if (window.readyState) { //older microsoft browsers
        window.onreadystatechange = function() {
            if (this.readyState == 'complete' || this.readyState == 'loaded') {
                cascadeJQLoad();
            }
        }
    } else { //modern browsers
        cascadeJQLoad();
    }
    

提交回复
热议问题