Skip to content

Instantly share code, notes, and snippets.

@getify
Created November 10, 2010 13:22
Show Gist options
  • Save getify/670840 to your computer and use it in GitHub Desktop.
Save getify/670840 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
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}
]);
@jeanph01
Copy link

How would you adapt the below script to use with the fallback functionality ?

$LAB
.script("framework.js").wait()
.script("myplugin.framework.js").wait()
.script("init.js");

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment