Skip to content

Instantly share code, notes, and snippets.

@dperini
Created April 10, 2010 16:27
Show Gist options
  • Save dperini/362134 to your computer and use it in GitHub Desktop.
Save dperini/362134 to your computer and use it in GitHub Desktop.
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