Skip to content

Instantly share code, notes, and snippets.

@NKid
Last active May 12, 2021 05:07
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 NKid/81576c58ed3a09ff5021 to your computer and use it in GitHub Desktop.
Save NKid/81576c58ed3a09ff5021 to your computer and use it in GitHub Desktop.
[getScript]
function getScript(url, success) {
var script = document.createElement('script');
script.src = url;
var head = document.getElementsByTagName('head')[0],
done = false;
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function() {
if(!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
//example
getScript('http://code.jquery.com/jquery.min.js', function() {
if(typeof jQuery == 'undefined') {
alert('Load Failed');
} else {
alert('Load Success. (jQuery v' + jQuery.fn.jquery + ')');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment