Javascript ajax调用后在哪里加载脚本?(重复)

Javascript ajax调用后在哪里加载脚本?(重复),javascript,ajax,jquery,Javascript,Ajax,Jquery,在一个案例中,我们找到了一个解决方案,解决了动态加载带有嵌入式标记的页面的问题。然而,正如我逐渐发现的那样,这并不是一个全面的解决方案。破坏代码的场景(请查看上一篇文章及其解决方案)如下所示: // inspired by Kevin B's suggestions // at: http://stackoverflow.com/questions/8234215/where-are-scripts-loaded-after-an-ajax-call (function ($) { $

在一个案例中,我们找到了一个解决方案,解决了动态加载带有嵌入式
标记的页面的问题。然而,正如我逐渐发现的那样,这并不是一个全面的解决方案。破坏代码的场景(请查看上一篇文章及其解决方案)如下所示:

// inspired by Kevin B's suggestions
// at: http://stackoverflow.com/questions/8234215/where-are-scripts-loaded-after-an-ajax-call

(function ($) {
    $.fn.loadWithJs = function (o) {
        console.log(".loadWithJs - started");
        var target = this;

        target.addScript = function (scripts, n, success) {
            var script = scripts[n];

            var scriptNode = document.createElement('script');
            for (var i = 0; i < script.attributes.length; i++) {
                var attr = script.attributes[i];
                $(scriptNode).attr(attr.name, attr.value);
            }
            scriptNode.appendChild(
                document.createTextNode($(script).html())
            );
            $(scriptNode).load(function () {
                if (++n < scripts.length)
                    target.addScript(scripts, n, success);
                else if (success)
                    success();
            });
            target[0].appendChild(scriptNode);
        };

        var success = o.success;
        o.success = function (html) {
            // convert script tags
            var h = $('<div />').append(
                html.replace(/<(\/)?script/ig, "<$1codex")
            );
            // detach script divs from html
            var scripts = h.find("codex").detach();
            // populate target
            target.html(h.children());
            // start recursive loading of scripts
            target.addScript(scripts, 0, success);
        };

        $.ajax(o);
    };
})(jQuery);
-svc.html-

<script type="text/javascript" src="/plugin.js" css="/plugin.css"></script>
<script type="text/javascript">
plugin_method();
</script>

<div id='inner'>
dynamically loaded content
</div>
代码不会立即执行。如果(如前一篇文章中所述)svc.html页面中只有一个脚本标记,则一切正常,但一旦有了第二个,插件中的
$('script:last')
就不再指向正确的脚本,因此无法加载样式表

此外,第二个脚本标记的主体似乎在第一个脚本的代码源中之前执行,因此方法调用失败(因为函数尚未定义)

我发现了几个链接,我正在仔细考虑:


有人有想法吗?

好的,所以答案是我们不能只遍历脚本列表-我们需要在加载事件中递归,如下所示:

// inspired by Kevin B's suggestions
// at: http://stackoverflow.com/questions/8234215/where-are-scripts-loaded-after-an-ajax-call

(function ($) {
    $.fn.loadWithJs = function (o) {
        console.log(".loadWithJs - started");
        var target = this;

        target.addScript = function (scripts, n, success) {
            var script = scripts[n];

            var scriptNode = document.createElement('script');
            for (var i = 0; i < script.attributes.length; i++) {
                var attr = script.attributes[i];
                $(scriptNode).attr(attr.name, attr.value);
            }
            scriptNode.appendChild(
                document.createTextNode($(script).html())
            );
            $(scriptNode).load(function () {
                if (++n < scripts.length)
                    target.addScript(scripts, n, success);
                else if (success)
                    success();
            });
            target[0].appendChild(scriptNode);
        };

        var success = o.success;
        o.success = function (html) {
            // convert script tags
            var h = $('<div />').append(
                html.replace(/<(\/)?script/ig, "<$1codex")
            );
            // detach script divs from html
            var scripts = h.find("codex").detach();
            // populate target
            target.html(h.children());
            // start recursive loading of scripts
            target.addScript(scripts, 0, success);
        };

        $.ajax(o);
    };
})(jQuery);
//受Kevin B的建议启发
//地址:http://stackoverflow.com/questions/8234215/where-are-scripts-loaded-after-an-ajax-call
(函数($){
$.fn.loadWithJs=函数(o){
log(“.loadWithJs-started”);
var目标=此;
target.addScript=函数(脚本,n,成功){
var script=脚本[n];
var scriptNode=document.createElement('script');
对于(var i=0;i