public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<html>
<head>
<script type="text/javascript" src="LAB.js"></script>
<script type="text/javascript">
var _queue = ["script1.js",null], $L = $LAB;
</script>
 
...
</head>
<body>
<script type="text/javascript">
if (something) { // script 2 going to be loaded
_queue.push("script2-a.js","script2-b.js",function(){
initScript1();
initScript2();
});
}
else { // script 2 not going to be loaded, so just init script-1
_queue.push(function(){
initScript1();
});
}
 
</script>
 
...
 
<script type="text/javascript">
if (somethingElse) {
_queue.push("script-3.js",function(){
initScript3();
});
}
</script>
 
...
 
<script type="text/javascript">
 
for (var i=0, len=_queue.length; i<len; i++) {
if (typeof _queue[i] == "string") { // script string source found
$L = $L.script(_queue[i]);
}
else if (!_queue[i]) { // null/false found
$L = $L.wait();
}
else if (typeof _queue[i] == "function") { // inline function found
$L = $L.wait(_queue[i]);
}
}
 
</script>
 
</body>
</html>

This gist shows how to conditionally build up the .script and .wait calls into an array, and simulate the chaining with a for-loop

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.