Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.head.appendChild(element) ie ie7 and ie8

i am having an issue appending a script to the head in ie7/8

this is the code i am using

var requireTag = document.createElement('script');
requireTag.setAttribute('type',         'text/javascript');
requireTag.setAttribute('src',          link+ 'require.js');
requireTag.setAttribute('data-main',    link+ 'data');

document.head.appendChild(requireTag);

this is the error i get

SCRIPT5007: Unable to get value of the property
'appendChild': object is null or undefined  

I found this createElement error in IE8 and tried updating my code to have

var appendChild = document.head.appendChild(requireTag);

but still get the same error. Can anyone help?

like image 368
Dan Avatar asked Jun 14 '13 02:06

Dan


2 Answers

According to https://developer.mozilla.org/en-US/docs/Web/API/document.head and http://msdn.microsoft.com/en-us/library/gg593004%28v=vs.85%29.aspx , document.head isn't available to IE<9. Just use

document.getElementsByTagName('head')[0].appendChild(requireTag);
like image 117
Musa Avatar answered Nov 10 '22 17:11

Musa


I believe document.head isn't supported in those browsers.

Try this instead:

var head = document.getElementsByTagName("head")[0];
head.appendChild(requireTag);
like image 24
user2437417 Avatar answered Nov 10 '22 18:11

user2437417