Created
April 10, 2010 16:27
-
-
Save dperini/362134 to your computer and use it in GitHub Desktop.
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
var loadScript = (function(global) { | |
return function(url) { | |
var script, | |
// document reference | |
doc = global.document, | |
// root element reference | |
root = doc.documentElement, | |
// head element reference to add scripts | |
head = doc.getElementsByTagName('head')[0] || root; | |
// inject a script as first element in the head section | |
script = head.insertBefore(doc.createElement('script'), head.firstChild); | |
script.type = 'text/javascript'; | |
script.src = url; | |
// need a small IE inference :( | |
// to avoid problems with Opera | |
// doesn't this make sense :?) | |
if (doc.createEventObject) { | |
// setup script 'readystatechange' handler | |
script.onreadystatechange = | |
function() { | |
if (/loaded/.test(script.readyState)) { | |
init(); | |
} | |
}; | |
} else { | |
// setup script 'load' handler | |
script.onload = init; | |
} | |
}; | |
})(this); | |
function init() { | |
// do stuff here after script has loaded | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment