最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

adding a script tag to the DOM with javascript in IE with data attribute` - Stack Overflow

programmeradmin2浏览0评论

I need to add the following script tag to the DOM after a few things run on my page:

<script data-main="js/main" src="lib/Require/require.js"></script>

I know that optimally everything will be in my require file, but as of now i need to hotfix this to work in IE.

What i have that is working in FF/Chrome is:

var script = document.createElement('script');
script.setAttribute('data-main', 'js/main');
script.src = 'lib/Require/require.js';
document.getElementsByTagName('script')[0].parentNode.appendChihld(script);

However, IE doesn't like it when i try to set the attribute 'data-main' and therefore is not working.

How can i get around this and have it add the script element to the dom and have it load the script at the same time?

Thanks

I need to add the following script tag to the DOM after a few things run on my page:

<script data-main="js/main" src="lib/Require/require.js"></script>

I know that optimally everything will be in my require file, but as of now i need to hotfix this to work in IE.

What i have that is working in FF/Chrome is:

var script = document.createElement('script');
script.setAttribute('data-main', 'js/main');
script.src = 'lib/Require/require.js';
document.getElementsByTagName('script')[0].parentNode.appendChihld(script);

However, IE doesn't like it when i try to set the attribute 'data-main' and therefore is not working.

How can i get around this and have it add the script element to the dom and have it load the script at the same time?

Thanks

Share Improve this question asked Jan 30, 2013 at 3:25 Troy CosentinoTroy Cosentino 4,77810 gold badges40 silver badges61 bronze badges 1
  • Probably it's just the transcription in here, but you have an extra h in appendChihld. – bfavaretto Commented Jan 30, 2013 at 3:47
Add a comment  | 

1 Answer 1

Reset to default 26

this seems to work fine in IE as well:

var scriptTag = document.createElement("script");
scriptTag.type = "text/javascript";
scriptTag.src = "lib/Require/require.js";
scriptTag.setAttribute("data-main", "js/main");
( document.getElementsByTagName("head")[0] || document.documentElement ).appendChild( scriptTag );
发布评论

评论列表(0)

  1. 暂无评论