[英]Trying to inject javascript into my page
For reasons too complicated to get into now, I have an ajax call that returns some dynamically created Javascript that I want to inject into my page. 由于太复杂而无法进入的原因,我有一个ajax调用,它返回一些动态创建的Javascript,我想将其注入到我的页面中。 The following code works on Chrome, but not in IE:
以下代码适用于Chrome,但不适用于IE:
var node = document.getElementsByTagName("head")[0] || document.body;
if (node)
{
var script = document.createElement("script");
script.type = "text/javascript";
//script.innerHTML = json.javascript;
var textnode = document.createTextNode(json.javascript);
script.appendChild(textnode);
node.appendChild(script);
}
In IE, I get "SCRIPT65535: Unexpected call to method or property access." 在IE中,我得到“SCRIPT65535:对方法或属性访问的意外调用。” As you can see from the commented out code, before I tried the textnode, I tried just inserting it with script.innerHTML.
从注释掉的代码中可以看出,在我尝试使用textnode之前,我尝试使用script.innerHTML插入它。 That also worked in Chrome, but in IE I got "SCRIPT600:Unknown runtime error".
这也适用于Chrome,但在IE中我得到了“SCRIPT600:未知的运行时错误”。
Is there a way to stick some javascript into the DOM in IE? 有没有办法在IE中将一些javascript粘贴到DOM中?
And of course, as soon as I post this, I find http://www.phpied.com/dynamic-script-and-style-elements-in-ie/ 当然,只要我发布这个,我就会找到http://www.phpied.com/dynamic-script-and-style-elements-in-ie/
var node = document.getElementsByTagName("head")[0] || document.body;
if (node)
{
var script = document.createElement("script");
script.type = "text/javascript";
script.text = json.javascript;
node.appendChild(script);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.