Do something once jQuery plugin has loaded

后端 未结 3 1506
不知归路
不知归路 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:39
    <script src="jquery ui"></script>
    <!-- jQuery UI has been initialized -->
    <script src="my source"></script>
    
    0 讨论(0)
  • 2021-01-18 13:40

    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);
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题