Instantly share code, notes, and snippets.

Embed
What would you like to do?
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