Skip to content

Instantly share code, notes, and snippets.

@samuelcole
Created April 13, 2010 15:50
Show Gist options
  • Select an option

  • Save samuelcole/364746 to your computer and use it in GitHub Desktop.

Select an option

Save samuelcole/364746 to your computer and use it in GitHub Desktop.
zero dependency $.getScript for loading large external javascripts
// zero dependency $.getScript for loading external javascript (like jQuery.js
// or other big libraries) in a way that does not block the normal page load
// (http://www.yuiblog.com/blog/2008/07/22/non-blocking-scripts/).
//
// heavily influenced by/copied from jQuery's ajax functions (http://jquery.com)
window.$ = {
getScript: function(script_src, callback) {
var done = false;
var head = document.getElementsByTagName("head")[0] || document.documentElement;
var script = document.createElement("script");
script.src = script_src;
script.onload = script.onreadystatechange = function() {
if ( !done && (!this.readyState ||
this.readyState === "loaded" || this.readyState === "complete") ) {
if(callback) callback();
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
if ( head && script.parentNode ) {
head.removeChild( script );
}
done = true;
}
};
head.insertBefore( script, head.firstChild );
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment