简体   繁体   English

注入的JavaScript不会执行

[英]Injected javascript does not execute

I'm trying to write a piece of javascript that integrates a webpage with an external system. 我正在尝试编写一个将网页与外部系统集成的JavaScript。 Currently, what I have is a very small amount of code that dynamically inserts a script element into the page. 目前,我所拥有的是一个非常少量的代码,可以动态地将脚本元素插入到页面中。

$(document).ready(function() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://example.com/example.php?arg1=1&arg2=2";
    document.body.appendChild(script);
});

The JS returned by the external system is a large one-line document.write call which writes a table to the page. 外部系统返回的JS是一个大型的单行document.write调用,它将表写入页面。

document.write('<table><tr><td>blah</td></tr></table>');

When I inject this script element into the page, I can see it using a DOM inspector, but the script does not appear to execute. 当我将这个脚本元素注入页面时,我可以使用DOM检查器看到它,但脚本似乎没有执行。 If I put the script element into the HTML manually, the javascript executes just fine (so it's not a same origin policy or malformed html error...), but I'd like it to be dynamic. 如果我手动将脚本元素放入HTML中,则javascript执行得很好(因此它不是相同的原始策略或格式错误的HTML错误...),但我希望它是动态的。

Any ideas why the javascript isn't executing? 任何想法为什么javascript没有执行?

Using document.write after the DOM is ready will replace the contents of the page with whatever it is you're writing. 在DOM准备好之后使用document.write将用你正在编写的内容替换页面的内容。

I suggest using one of the actual DOM manipulation methods if you want to insert anything into a page. 如果你想在页面中插入任何东西,我建议使用一种实际的DOM操作方法。

As far as the script not executing, are you positive it's being attached correctly? 至于脚本没有执行,你肯定是正确附加吗? Have you tried setting a javascript breakpoint on the included script to verify that this is the case? 您是否尝试在包含的脚本上设置javascript断点以验证是否是这种情况?

Because you are just including it, not executing. 因为你只是包含它,而不是执行。 As you are using jQuery, take a look in the $.getScript() function. 在使用jQuery时,请查看$.getScript()函数。

http://api.jquery.com/jQuery.getScript/

It will fit your needs. 它将满足您的需求。 This function is an Ajax function, so take care, because its behavior is asynchronous. 这个函数是一个Ajax函数,所以要小心,因为它的行为是异步的。 Use its callbacks to execute code that is based in the loaded script. 使用其回调来执行基于加载脚本的代码。

Edit: Felix corrected me about the script execution, but I still think that you may give the function a try. 编辑: Felix纠正了我关于脚本执行的问题,但我仍然认为你可以试试这个功能。

Try to use this code (it the same use by google for analytics or facebook). 尝试使用此代码(谷歌用于分析或facebook)。

Put it on the bottom of your page ;) 把它放在你的页面底部;)

<script type="text/javascript">
    (function() {
        var script = document.createElement('script'); 
            script.type = 'text/javascript'; 
            script.async = true;
        script.src = '/example.php?arg1=1&arg2=2';
        var s = document.getElementsByTagName('script')[0]; 
            s.parentNode.insertBefore(script, s);
    })();
</script>

OR as davidbuzatto suggest, you have to use $.getScript() which is a shorthand $.ajax() function. 或者像davidbuzatto建议的那样,你必须使用$ .getScript()这是一个简写的$ .ajax()函数。

$(document).ready(function() {
    $.getScript("/example.php?arg1=1&arg2=2", function(data, textStatus, jqxhr) {
        console.log(data); //data returned
        console.log(textStatus); //success
        console.log(jqxhr.status); //200
        console.log('Load was performed.');
   });
});

Edit : Seens you have probably a cross-domain restriction, just try to use relative url "/example.php?arg1=1&arg2=2" instead of the full url for the include. 编辑:看到你可能有跨域限制,只是尝试使用相对网址“/example.php?arg1=1&arg2=2”而不是包含的完整网址。 Or if it's not the same web server, use a cross-domain.xml file. 或者,如果它不是同一个Web服务器,请使用跨域domain.xml文件。

i think you need to use JSONP to achieve that and using a call back function to append it to body tag 我认为您需要使用JSONP来实现它并使用回调函数将其附加到body标签

$(document).ready(function() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://example.com/example.php?arg1=1&arg2=2&callback=showit(data)";
document.body.appendChild(script);
});

 function showit(data){document.write(data);}

Why not just stick the JS in a separate window/iframe where it executes and displays the tables correctly, and then pull that table using AJAX to that page? 为什么不直接将JS放在单独的窗口/ iframe中执行并正确显示表格,然后使用AJAX将该表拉到该页面?

JQuery AJAX is the easiest to use IMO: JQuery AJAX是最容易使用的IMO:

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

I've always seen this done more like: 我总是看到这样做更像:

document.write('<scr' + 'ipt>---scripthere---' + '</scr' + 'ipt>');

I'm assuming it is for similar reasons. 我假设是出于类似的原因。 Try that instead of creating a "real" script element. 尝试而不是创建一个“真正的”脚本元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM