Skip to content

Instantly share code, notes, and snippets.

@A35G
Last active August 29, 2015 14:08
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 A35G/4cdb97e02d74ecf17013 to your computer and use it in GitHub Desktop.
Save A35G/4cdb97e02d74ecf17013 to your computer and use it in GitHub Desktop.
How to detect if an external library has loaded
window.create_widget = function() {
if (jQuery("#box-dynamic").length)
$('#box-dynamic').html("Oh Yes");
}
window.loadjQuery = function(url, success) {
var script = document.createElement('script');
script.src = url;
var head = document.getElementsByTagName('head')[0],
done = false;
head.appendChild(script);
// 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);
}
};
}
if (typeof jQuery == 'undefined') {
loadjQuery('http://code.jquery.com/jquery-2.1.1.min.js', function() {
create_widget();
});
} else {
create_widget();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment