document.head.appendChild or document.createElemen

2019-04-04 05:30发布

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!

1条回答
倾城 Initia
2楼-- · 2019-04-04 06:05

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

查看更多
登录 后发表回答