I\'m dynamically loading jQuery
and jQuery UI
into a page, and I need to know when jQuery UI
has successfully extended jQuery
<script src="jquery ui"></script>
<!-- jQuery UI has been initialized -->
<script src="my source"></script>
I used this code on some project to ensure that some scripts run sequentially. It did not work on IE (and I didn't have time to debug), but worked fine on everything else.
var node = document.createElement("script");
$.extend(node, {
type: 'text/javascript',
charset: 'utf-8',
async: false,
defer: true,
src: /*...*/
});
$(node).bind('load', loadedCallback);
$('head')[0].appendChild(node);
I think this will do exactly what you need, add more dependency as much as you like.
(function () {
function getScript(url, success) {
var script = document.createElement('script');
script.src = url;
var head = document.getElementsByTagName('head')[0];
var completed = false;
script.onload = script.onreadystatechange = function () {
if (!completed && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
completed = true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
getScript("Scripts/jquery-1.6.1.js", function () {
getScript("Scripts/jquery-ui-1.8.11.js", function () {
alert($.ui);
});
});
})();
Tested with IE7, IE8, IE9, Firefox, Safari, Chrome and Opera