I’m using the asynchronous example from http://css-tricks.com/snippets/jquery/load-jquery-only-if-not-present/ 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 = '//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js';
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
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}; // End !done && this.readystate
}; // End script.online = onreadystate
head.appendChild(script);
}; // 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
}