在IE8中创建脚本标记 [英] Create script tag in IE8

查看:88
本文介绍了在IE8中创建脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我们的网站,在IE8中得到了可怕的对方法或属性访问的意外调用。错误。

I was testing our site, in IE8 and got the dreaded Unexpected call to method or property access. error.

经过大量调试(IE8的devtools糟透了)后,我发现了违规行。

After lots of debugging (IE8's devtools suck), I found the offending line.

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

问题是 $( '<脚本>')。HTML(JSData)。我试着在控制台中运行它,但我仍然遇到错误。

The problem is $('<script>').html(JSData). I tried running just that in the console, and I still got the error.

为什么IE8无法设置 .html 在新创建的脚本标签上?

Why can't IE8 set the .html on a newly created script tag?

PS这也失败了:

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

UPDATE :我试图在没有jQuery的情况下创建脚本标记:

UPDATE: I tried to create the script tag without jQuery:

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

上scriptTag.innerHTML = JSData; 行,IE8给出未知的运行时错误。谢谢IE8。

On the scriptTag.innerHTML = JSData; line, IE8 gives Unknown runtime error. Thanks IE8.

推荐答案

你的javascript方法需要将脚本元素添加到文档中。

Your javascript only method needs to add the script element to the document.

IE< 9无法识别脚本标记上的innerHTML或childNodes,但所有浏览器都支持text属性。

IE<9 does not recognize innerHTML or childNodes on script tags, but all browsers support the text property.

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

这篇关于在IE8中创建脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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