简体   繁体   English

ie6/7 脚本标记填充给出“对方法或属性访问的意外调用”

[英]ie6/7 script tag population giving “unexpected call to method or property access”

I use AJAX to get the content of a script, and then use the following code:我使用 AJAX 来获取脚本的内容,然后使用以下代码:

    var scr = document.createElement('script');
    scr.appendChild(document.createTextNode(script)); // ***
    document.getElementsByTagName('head')[0].appendChild(scr);

Where script is astring populated from AJAX.其中script是从 AJAX 填充的字符串。 This works fine in IE9, Chrome and Firefox.这在 IE9、Chrome 和 Firefox 中运行良好。 However, in IE6 and 7 I get an error:但是,在 IE6 和 7 中出现错误:

Unexpected call to method or property access对方法或属性访问的意外调用

IE gives the number of the the line indicated with the // *** . IE 给出了用// ***指示的行号。

Although there are multiple other questions about this, none of the appear to address this precise issue.尽管还有很多其他问题,但似乎都没有解决这个确切的问题。

Older IE's do not accept child nodes in script elements ( or in style and option elements, but that's another two questions).较旧的 IE 不接受脚本元素(或样式和选项元素,但这是另外两个问题)中的子节点。

You can set the script element's text property instead.您可以改为设置脚本元素的文本属性。 (scripttext is a string of, well, script text.) (scripttext 是一串脚本文本。)

var scr = document.createElement('script');
if(window.addEventListener)scr.appendChild(document.createTextNode(script))
else scr.text=scripttext;
document.getElementsByTagName('head')[0].appendChild(scr);

If you already have the code in a string, why make a script tag out of it?如果您已经在字符串中包含代码,为什么要从中制作脚本标记? Can't you just call eval(script) on it.你不能只调用eval(script)就可以了。 Won't that do the same thing?那不会做同样的事情吗?

document.getElementsByTagName('head')[0] * ; document.getElementsByTagName('head')[0] * ; * .appendChild(scr); * .appendChild(scr);

Why did you put a semicolon here?你为什么在这里放一个分号?

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

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