-
-
Save englishextra/5bce24b8b4276c19c33d5bbf64453222 to your computer and use it in GitHub Desktop.
using LABjs to load from a CDN, with simple error detection (& timeout), and a local load fallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test_jQuery() { jQuery(""); } | |
function success_jQuery() { alert("jQuery is loaded!"); | |
var successfully_loaded = false; | |
function loadOrFallback(scripts,idx) { | |
function testAndFallback() { | |
clearTimeout(fallback_timeout); | |
if (successfully_loaded) return; // already loaded successfully, so just bail | |
try { | |
scripts[idx].test(); | |
successfully_loaded = true; // won't execute if the previous "test" fails | |
scripts[idx].success(); | |
} catch(err) { | |
if (idx < scripts.length-1) loadOrFallback(scripts,idx+1); | |
} | |
} | |
if (idx == null) idx = 0; | |
$LAB.script(scripts[idx].src).wait(testAndFallback); | |
var fallback_timeout = setTimeout(testAndFallback,10*1000); // only wait 10 seconds | |
} | |
loadOrFallback([ | |
{src:"http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js", test:test_jQuery, success:success_jQuery), | |
{src:"/local/jquery-1.4.min.js", test:test_jQuery, success:success_jQuery} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment