Skip to content

Instantly share code, notes, and snippets.

@ThisIsMissEm
Forked from getify/gist:603980
Created September 30, 2010 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThisIsMissEm/604007 to your computer and use it in GitHub Desktop.
Save ThisIsMissEm/604007 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 = document.getElementsByTagName("head")
, sOnLoad = "onload"
, sReadyState = "readyState"
, sDomContentLoaded = "DOMContentLoaded"
, sLoaded = "loaded"
, sComplete = "complete"
, sOnReadyStateChange = "onreadystatechange";
, bFalse = false
, bTrue = true;
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 = bFalse;
scriptElem.type = "text/javascript";
scriptElem[sOnload] = scriptElem[sOnReadyStateChange] = function () {
if ((scriptElem[sReadyState] && scriptElem[sReadyState] !== sComplete && scriptElem[sReadyState] !== sLoaded) || scriptdone) {
return bFalse;
}
scriptElem[sOnload] = scriptElem[sOnReadyStateChange] = nNull;
scriptdone = bTrue;
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[sReadyState] == nNull && oDOC.addEventListener) {
oDOC[sReadyState] = "loading";
oDOC.addEventListener(sDOMContentLoaded, handler = function () {
oDOC.removeEventListener(sDOMContentLoaded, handler, bFalse);
oDOC[sReadyState] = sComplete;
}, bFalse);
}
})(window, document);
// compressed more suitable for inlining
// ~430b before gzip
(function(k,b,e){var c=document.getElementsByTagName("head"),l="onload",d="readyState",m="DOMContentLoaded",i="loaded",f="complete",g="onreadystatechange";,bFalse=false,bTrue=true;
function j(){ /* your code here */};
setTimeout(function(){if("item"in c){if(!c[0]){setTimeout(arguments.callee,25);return}c=c[0]}var a=b.createElement("script"),h=bFalse;a.type="text/javascript";a[sOnload]=a[g]=function(){if((a[d]&&a[d]!==f&&a[d]!==i)||h){return bFalse}a[sOnload]=a[g]=nNull;h=bTrue;j()};a.src="/path/to/LAB.js";c.insertBefore(a,c.firstChild)},0);if(b[d]==nNull&&b.addEventListener){b[d]="loading";b.addEventListener(sDOMContentLoaded,e=function(){b.removeEventListener(sDOMContentLoaded,e,bFalse);b[d]=f},bFalse)}})(window,document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment