document.write() not working in wordpress header.php

A javascript function which performs a document.write() works fine when called within a WordPress post body, but does nothing when called from within header.php template in WordPress.

Any suggestions on how to have document.write() work from within WordPress’ header.php?

Read More

switchads(); contains a document.write() command.
Called within header.php, the function runs, but document.write() produces no change to the page.

This snippet shows how the function (located in adtest.js) is called within header.php and it does not output HTML (in a post body, it does):

        <div id="ad1">
            <script type="text/javascript" src="adtest.js"></script>
            <script type="text/javascript">
            <!--//--><![CDATA[//><!--
            switchads();
            //--><!]]></script>
        </div>

javascript function being called contains a document.write():

function switchads() {
  document.write (
    '<div>random html here</div>'
  );
}

Related posts