注入的javascript无法执行 [英] Injected javascript does not execute

查看:87
本文介绍了注入的javascript无法执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一段将网页与外部系统集成在一起的JavaScript.目前,我所拥有的代码很少,可以将脚本元素动态地插入到页面中.

I'm trying to write a piece of javascript that integrates a webpage with an external system. 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);
});

外部系统返回的JS是一个大的单行document.write调用,它将表写入页面.

The JS returned by the external system is a large one-line document.write call which writes a table to the page.

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

当我将此脚本元素注入页面时,可以使用DOM检查器看到它,但是该脚本似乎没有执行.如果我将script元素手动放入HTML中,则javascript可以很好地执行(因此,它不是相同的原始策略或格式错误的html错误...),但是我希望它是动态的.

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. 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.

有什么想法为什么无法执行javascript?

Any ideas why the javascript isn't executing?

推荐答案

在DOM准备就绪后使用document.write会将页面内容替换为您正在编写的内容.

Using document.write after the DOM is ready will replace the contents of the page with whatever it is you're writing.

如果您想在页面中插入任何内容,建议使用一种实际的DOM操作方法.

I suggest using one of the actual DOM manipulation methods if you want to insert anything into a page.

就脚本未执行而言,您确定它已正确附加吗?您是否尝试过在随附的脚本上设置javascript断点以验证是否确实如此?

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无法执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆