Skip to content

Instantly share code, notes, and snippets.

@getify
Created September 30, 2010 03:39
Show Gist options
  • Star 69 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save getify/603980 to your computer and use it in GitHub Desktop.
Save getify/603980 to your computer and use it in GitHub Desktop.
// HOWTO: load LABjs itself dynamically!
// inline this code in your page to load LABjs itself dynamically, if you're so inclined.
(function (global, oDOC, handler) {
var head = oDOC.head || oDOC.getElementsByTagName("head");
function LABjsLoaded() {
// do cool stuff with $LAB here
}
// loading code borrowed directly from LABjs itself
setTimeout(function () {
if ("item" in head) { // check if ref is still a live node list
if (!head[0]) { // append_to node not yet ready
setTimeout(arguments.callee, 25);
return;
}
head = head[0]; // reassign from live node list ref to pure node ref -- avoids nasty IE bug where changes to DOM invalidate live node lists
}
var scriptElem = oDOC.createElement("script"),
scriptdone = false;
scriptElem.onload = scriptElem.onreadystatechange = function () {
if ((scriptElem.readyState && scriptElem.readyState !== "complete" && scriptElem.readyState !== "loaded") || scriptdone) {
return false;
}
scriptElem.onload = scriptElem.onreadystatechange = null;
scriptdone = true;
LABjsLoaded();
};
scriptElem.src = "/path/to/LAB.js";
head.insertBefore(scriptElem, head.firstChild);
}, 0);
// required: shim for FF <= 3.5 not having document.readyState
if (oDOC.readyState == null && oDOC.addEventListener) {
oDOC.readyState = "loading";
oDOC.addEventListener("DOMContentLoaded", handler = function () {
oDOC.removeEventListener("DOMContentLoaded", handler, false);
oDOC.readyState = "complete";
}, false);
}
})(window, document);
// compressed more suitable for inlining
// ~640b before gzip
(function(g,b,d){var c=b.head||b.getElementsByTagName("head"),D="readyState",E="onreadystatechange",F="DOMContentLoaded",G="addEventListener",H=setTimeout;
function f(){
// $LAB stuff here
}
H(function(){if("item"in c){if(!c[0]){H(arguments.callee,25);return}c=c[0]}var a=b.createElement("script"),e=false;a.onload=a[E]=function(){if((a[D]&&a[D]!=="complete"&&a[D]!=="loaded")||e){return false}a.onload=a[E]=null;e=true;f()};
a.src="/path/to/LAB.js";
c.insertBefore(a,c.firstChild)},0);if(b[D]==null&&b[G]){b[D]="loading";b[G](F,d=function(){b.removeEventListener(F,d,false);b[D]="complete"},false)}})(this,document);
@sajal
Copy link

sajal commented Mar 5, 2011

@getify: aha come to think of it it does make sense now to use the regular <script> tag for labjs.

Ive already working on a mechanism to bundle all self owned js including labjs into a single file, and all 3rd party js is handled by $LAB . No reason to not use <script> ... nothing more to block anyways...

I was getting a little too carried over trying to over-optimize.

@tobie
Copy link

tobie commented Jul 13, 2011

You probably mean: var head = oDOC.head || oDOC.getElementsByTagName("head")[0];

@getify
Copy link
Author

getify commented Jul 13, 2011

@tobie -- no, check lines 13-19 for how it does that

@tobie
Copy link

tobie commented Jul 13, 2011

Yuck.

@getify
Copy link
Author

getify commented Jul 13, 2011

@tobie -- it's to address an older IE quirk/bug where the [0] element is not yet set, and if you capture a ref to it too early, when it's made available, your ref will be invalid.

@tobie
Copy link

tobie commented Jul 13, 2011

Heh. Deserves a comment, don't you think?

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