简体   繁体   English

在IE8中创建脚本标记

[英]Create script tag in IE8

I was testing our site, in IE8 and got the dreaded Unexpected call to method or property access. 我在IE8中测试我们的网站,并获得了Unexpected call to method or property access.的可怕的Unexpected call to method or property access. error. 错误。

After lots of debugging (IE8's devtools suck), I found the offending line. 经过大量的调试(IE8的devtools糟透了),我发现了违规行。

$('<script>').html(JSData).appendTo('head')

The problem is $('<script>').html(JSData) . 问题是$('<script>').html(JSData) I tried running just that in the console, and I still got the error. 我试着在控制台中运行,我仍然得到了错误。

Why can't IE8 set the .html on a newly created script tag? 为什么IE8不能在新创建的脚本标签上设置.html

PS This fails too: PS这也失败了:

$(document.createElement('script')).html(JSData)

UPDATE : I tried to create the script tag without jQuery: 更新 :我试图在没有jQuery的情况下创建脚本标记:

var scriptTag = document.createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.innerHTML = JSData;

On the scriptTag.innerHTML = JSData; scriptTag.innerHTML = JSData; line, IE8 gives Unknown runtime error . 行,IE8给出了Unknown runtime error Thanks IE8. 谢谢IE8。

Your javascript only method needs to add the script element to the document. 您的仅javascript方法需要将脚本元素添加到文档中。

IE<9 does not recognize innerHTML or childNodes on script tags, but all browsers support the text property. IE <9无法识别脚本标记上的innerHTML或childNodes,但所有浏览器都支持text属性。

var scriptTag = document.createElement('script');
scriptTag.text= JSData;
document.body.appendChild(scriptTag);

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

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