Created
April 3, 2013 20:40
-
-
Save lukeasrodgers/5305060 to your computer and use it in GitHub Desktop.
function to load javascripts, based on https://gist.github.com/getify/603980
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
load_script = function (url, callback) { | |
var oDOC = document; | |
var head = oDOC.head || oDOC.getElementsByTagName("head"); | |
// 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; | |
if (typeof callback === 'function') { | |
callback(); | |
} | |
}; | |
scriptElem.src = url; | |
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"; | |
var handler; | |
oDOC.addEventListener("DOMContentLoaded", handler = function () { | |
oDOC.removeEventListener("DOMContentLoaded", handler, false); | |
oDOC.readyState = "complete"; | |
}, false); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment