Javascript 用外部脚本结果填充本地div

Javascript 用外部脚本结果填充本地div,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我在一个站点上使用Angular和JQuery,并将页眉和页脚设置为partials,以最大限度地重用。我遇到的问题是,我需要在加载时在页脚中处理一些javascript,但Angular通过ng include标记或带有$.load()的JQuery方法不支持这一点 我想运行多个第三方脚本,但我现在正在处理的是一个Verisign脚本,它返回一个图像,使用模式加载。我知道,至少目前Javascript无法引入外部脚本,因此我的想法是简单地创建一个容器div,在其中显示图像,从主页运行第三方脚本

我在一个站点上使用Angular和JQuery,并将页眉和页脚设置为partials,以最大限度地重用。我遇到的问题是,我需要在加载时在页脚中处理一些javascript,但Angular通过
ng include
标记或带有
$.load()的JQuery方法不支持这一点

我想运行多个第三方脚本,但我现在正在处理的是一个Verisign脚本,它返回一个图像,使用
模式加载。我知道,至少目前Javascript无法引入外部脚本,因此我的想法是简单地创建一个容器div,在其中显示图像,从主页运行第三方脚本,然后获取该内容并通过引用填充容器div,概念上如下所示:

//footer.html
<div id="siteVerify"></div>

//index.html
//call a method that takes 3rd party that i can point to siteVerify
并在index.html文件的底部调用它。同时,在我的页脚中,我有如下内容:

<div class='deadScriptContainer'><span id='siteseal'>
    <script type="text/javascript" src="https://seal.godaddy.com/getSeal?sealID=...."></script></span>
</div>

因此,它应该在类为“deadScriptContainer”的div中查找脚本标记,并查找其src,它应该是https:。。。。我在代码中添加了一个警报,它看起来确实在查找脚本标记,但是当我检查src值是什么时,它总是返回“undefined”


我有一种感觉,我的选择器可能是错的,但我看不出这是怎么回事。

当您使用AJAX加载脚本标记时,问题是它们被带到了您想要的位置,因此它不会启动。不过,有一种简单的方法可以让这些死去的脚本复活,所以你很幸运

从AJAX请求中获得数据后,请评估任何内部JavaScript

对于外部JavaScript,您需要做的是创建一个新的
标记,并为其提供死脚本的源代码

加载新数据后,整个过程应如下所示:

// If you know these dead scripts will be in a certain container, refine your jQuery selector
$('deadScriptContainer script').each(function(){
    // If a script has content, we want to execute it
    if($(this).html().length > 0){
        // Eval() will execute any JavaScript it is passed
        eval($(this).html());
    }else{
        // If this script has no content, let's see if it has a src
        if($(this).src){
            // Create a new script tag
            var newScript = document.createElement("script");
            newScript.type = "text/javascript";
            newScript.src = $(this).src;
            // Append new script to the head
            document.querySelector("head").appendChild(newScript);
        }
    }
});

如果我错了,请纠正我-但是当您从ajax加载新数据时,您基本上有问题吗?在新数据中有
标记,当您将这些标记放到页面上时,这些标记不会触发?还是我误解了?这是根本问题,是的。我需要在加载分部时对脚本进行评估,或者从主页运行脚本,获取内容并填充分部中的标记。从我读到的所有内容来看,不支持在加载部分时评估标记。请参阅我的答案,并让我知道这是否适用于您。因此,在这个过程中,我将为每个脚本添加一个名为“deadScriptContainer”的类属性?该容器基本上就是死脚本所在的位置。例如,如果您将所有新数据加载到某个div中,您希望将该div中的所有脚本标记作为目标。好的,我将脚本放在一个标记为“deadScriptContainer”的容器中,它将查看内部并找到任何要处理的脚本,只需将目标锁定在死脚本基本所在的位置。“DeadScriptContainer”是任意正确的,我是说基于您的示例,它不一定必须被称为“DeadScriptContainer”
// If you know these dead scripts will be in a certain container, refine your jQuery selector
$('deadScriptContainer script').each(function(){
    // If a script has content, we want to execute it
    if($(this).html().length > 0){
        // Eval() will execute any JavaScript it is passed
        eval($(this).html());
    }else{
        // If this script has no content, let's see if it has a src
        if($(this).src){
            // Create a new script tag
            var newScript = document.createElement("script");
            newScript.type = "text/javascript";
            newScript.src = $(this).src;
            // Append new script to the head
            document.querySelector("head").appendChild(newScript);
        }
    }
});