Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.head.appendChild or document.createElement not working in IE

I have a script running in the head of my html document and it works in every browser except for internet explorer. Tested in Opera, Safari, Chrome, Firefox, Internet Explorer.

My code is as follows:

<html>
  <head>
    <script type = "text/javascript">
      var date = new Date();
      var month = date.getMonth() + 1;
      if (month >= 3 && month <= 5)
      {
        var NewScript = document.createElement("script");
        NewScript.type = "text/javascript";
        NewScript.src = "source1.js";
        var NewStyles = document.createElement("link");
        NewStyles.rel = "stylesheet";
        NewStyles.type = "text/css";
        NewStyles.href = "css1.css";
        document.head.appendChild(NewScript);
        document.head.appendChild(NewStyles);
      }
      else
      {
        var NewScript = document.createElement("script");
        NewScript.type = "text/javascript";
        NewScript.src = "source2.js";
        var NewStyles = document.createElement("link");
        NewStyles.rel = "stylesheet";
        NewStyles.type = "text/css";
        NewStyles.href = "css2.css";
        document.head.appendChild(NewScript);
        document.head.appendChild(NewStyles);
      }
    </script>
  </head>
  <body>
  <!-- MY CONTENT GOES HERE -->
  </body>
</html>

I'm not sure if it's the document.createElement or document.head.appendChild that isn't working in IE. As stated before, it works in all other browsers that I've tested it in. Help with this would be greatly appreciated as I will continue to find the problem / solution myself. Thanks!

like image 306
Vince Boromeo Avatar asked Apr 15 '12 02:04

Vince Boromeo


1 Answers

Try document.getElementsByTagName('head')[0] instead of document.head

like image 140
stewe Avatar answered Nov 15 '22 10:11

stewe