Create script tag in IE8

痞子三分冷 提交于 2019-12-30 03:49:06

问题


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

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

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

The problem is $('<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?

P.S. This fails too:

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

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

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

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


回答1:


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

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);


来源:https://stackoverflow.com/questions/12201485/create-script-tag-in-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!