Created
September 4, 2012 12:58
-
-
Save mjschwartz/3620928 to your computer and use it in GitHub Desktop.
Cross-browser asynchronous JS script loader w/callbacks
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
// getScript() | |
// more or less stolen form jquery core and adapted by paul irish | |
function getScript(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; | |
success(); | |
} | |
}; | |
head.appendChild(script); | |
} | |
// usage: | |
getScript('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