Vince Boromeo
Vince Boromeo

Reputation: 181

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!

Upvotes: 18

Views: 27194

Answers (2)

BenC
BenC

Reputation: 1737

Try document.head.appendChild instead of append

Upvotes: 0

stewe
stewe

Reputation: 42654

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

Upvotes: 44

Related Questions