Help with loading backups of JS files from CDN

I’m using the asynchronous example from to load a backup jQuery CDN if the main Google fails or goes undefined for some reason. The default script loads the backup (ASPNET CDN) but if I want to add a local copy backup to the ASPNET CDN, it will not load if the ASPNET CDN is down. I tried different variations with no success. Here is the code:

// Setup backups
var jqbackup1 = '//';
var jqbackup2 = '/js/jquery-1.4.4.min.js';

// Setup getScript function
function getScript(url, success) {
    var script = document.createElement('script');
    script.src = url;
    var head = document.getElementsByTagName('head')[0],
    done = false;
    // Attach handlers for all browsers
    script.onload = script.onreadystatechange = function() {
        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
            done = true;
            // callback function provided as param
            script.onload = script.onreadystatechange = null;

        }; // End !done && this.readystate

    }; // End = onreadystate

}; // End getScript function

// If main Google jQuery load is undefined or does not exist
if (typeof jQuery == 'undefined') {
    // If another JS lib like mootools is using $
    if (typeof $ == 'function') {
        // warning, global var
        thisPageUsingOtherJSLibrary = true;
    // Load first backup
    getScript(jqbackup1, function() {
        // If first backup fails
        if (typeof jQuery == 'undefined') {
            // First backup fails, so load local copy
            getScript(jqbackup2, function() {
                // Should anything extra be here?
        // First backup worked
        } else {
            // Run jQuery code
    }); // End getscript for jqbackup1

// Main Google jQuery loaded fine
} else {
    // Run your jQuery Code

I am guessing that maybe this block of code is giving me the issues because either the undefined check isn’t seeing it or the getscript for jqbackup2 is trying to access something that may or may not be undefined anymore or is too deep.

I’ve tested putting in wrong version numbers to force the script to call in the backups, but when I fudge the version number for jqbackup1, the local copy (jqbackup2) is not loading according to the Net area of Firebug and the scripts using jQuery.

    [COLOR=#ff5555][I]// Load first backup[/I][/COLOR]
    getScript[COLOR=#66cc66]([/COLOR]jqbackup1, [COLOR=#003366][B]function[/B][/COLOR][COLOR=#66cc66]([/COLOR][COLOR=#66cc66])[/COLOR] [COLOR=#66cc66]{[/COLOR]
        [COLOR=#ff5555][I]// If first backup fails[/I][/COLOR]
        [COLOR=#000066][B]if[/B][/COLOR] [COLOR=#66cc66]([/COLOR][COLOR=#000066][B]typeof[/B][/COLOR] jQuery == [COLOR=#3366cc]'undefined'[/COLOR][COLOR=#66cc66])[/COLOR] [COLOR=#66cc66]{[/COLOR]
            [COLOR=#ff5555][I]// First backup fails, so load local copy[/I][/COLOR]
            getScript[COLOR=#66cc66]([/COLOR]jqbackup2, [COLOR=#003366][B]function[/B][/COLOR][COLOR=#66cc66]([/COLOR][COLOR=#66cc66])[/COLOR] [COLOR=#66cc66]{[/COLOR]
                [COLOR=#ff5555][I]// Should anything extra be here?[/I][/COLOR]
        [COLOR=#ff5555][I]// First backup worked[/I][/COLOR]
        [COLOR=#66cc66]}[/COLOR] [COLOR=#000066][B]else[/B][/COLOR] [COLOR=#66cc66]{[/COLOR]
            [COLOR=#ff5555][I]// Run jQuery code[/I][/COLOR]
    [COLOR=#66cc66]}[/COLOR][COLOR=#66cc66])[/COLOR]; [COLOR=#ff5555][I]// End getscript for jqbackup1[/I][/COLOR]

It may not be possible for the web browser to process the updated DOM and load the script while the script is already running.

One possible solution to that is to delay the check using setTimeout

Another solution is to not rely on javascript for the task, and to instead request the jQuery library from a separate PHP script that attempts to retrieve the library from multiple sources.