Skip to content

Instantly share code, notes, and snippets.

@selbyk
Forked from ryanmcgrath/load_proper.js
Created May 14, 2014 18:47
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 selbyk/4ae44e609e0286605a30 to your computer and use it in GitHub Desktop.
Save selbyk/4ae44e609e0286605a30 to your computer and use it in GitHub Desktop.
/* Since script loading is dynamic, we take
a callback function with our loadScript call
that executes once the script is done downloading/parsing
on the page.
*/
var loadScript = function(src, callbackfn) {
var newScript = document.createElement("script");
newScript.type = "text/javascript";
newScript.setAttribute("async", "true");
newScript.setAttribute("src", src);
if(newScript.readyState) {
newScript.onreadystatechange = function() {
if(/loaded|complete/.test(newScript.readyState)) callbackfn();
}
} else {
newScript.addEventListener("load", callbackfn, false);
}
document.documentElement.firstChild.appendChild(newScript);
}
if(a) {
loadScript("lulz.js", function() { ... });
} else {
loadScript("other_lulz.js", function() { ... });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment