18

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!

Vince Boromeo
  • 181
  • 1
  • 1
  • 3

2 Answers2

44

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

stewe
  • 41,820
  • 13
  • 79
  • 75
  • 1
    Oh my gosh! You're a life saver! I tried almost everything but that. I'm creating different themes based on the seasons of the year, then my next project is holiday themes. Thanks again! – Vince Boromeo Apr 15 '12 at 03:00
0

Try document.head.appendChild instead of append

BenC
  • 1,647
  • 18
  • 25