Do something once jQuery plugin has loaded

后端 未结 3 1513
不知归路
不知归路 2021-01-18 13:11

I\'m dynamically loading jQuery and jQuery UI into a page, and I need to know when jQuery UI has successfully extended jQuery

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 13:46

    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

提交回复
热议问题