Skip to content

Instantly share code, notes, and snippets.

@freekrai
Created November 20, 2015 07:43
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 freekrai/7d050b225c55746ef5f7 to your computer and use it in GitHub Desktop.
Save freekrai/7d050b225c55746ef5f7 to your computer and use it in GitHub Desktop.
include() - load scripts asynchronously w/375 bytes of code - e.g. `include(src, cb)`
function include(url,success) {
var head = document.getElementsByTagName("head")[0], done = false;
var script = document.createElement("script");
script.src = url;
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function(){
if (!done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") ) {
done = true;
if (typeof success === 'function') success();
}
};
head.appendChild(script);
}
// usage:
include('http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js',function(){
alert('jQuery loaded. yay life')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment