none
script elements written with document.write in a window opened with window.open are not executed on first page load

    Question

  • I recently got a new laptop with Windows 7 64-bit.  A project I'm working on, which had been working in IE8 on XP, didn't work when I tried it on the new machine.

    In javascript, I'm creating a new window with window.open(), then using document.write() to write the contents of that new window, which contains script includes. In IE 8 on Windows 7, these scripts are not being executed properly. Most of the time they don't execute at all, but occasionally one of them will. This is only with a cleared cache - once the javascript files are in the cache, it works fine.

    Here is a boiled down test case.

    test.html:

    <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
    <html>
    <head>
     <script type="text/javascript">
      var w = window.open();
      var windowHTML =
       "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>\n" +
       "<html>\n" +
       "<head>\n" +
       " <script type='text/javascript' src='test.js'></scr"+"ipt>\n" +
       " <script type='text/javascript' src='test2.js'></scr"+"ipt>\n" +
       "</head>\n" +
       "<body>\n" +
       "</body>\n" +
       "</html>";
      w.document.write(windowHTML);
      w.document.close();
     </script>
    </head>
    <body>
    </body>
    </html>
    

    test.js:

    alert("test");
    

    test2.js:

    alert("test2");
    
    When I go to test.html, I would expect to see a new window pop up with alerts for "test" then "test2". In all other browsers I've tried (Firefox, Chrome, Safari, Opera, and IE 6, 7, & 8 on pre-Windows 7 systems) it works.  However, when I try this in IE8 on Windows 7, it opens the blank page, but no alerts appear (or occasionally just one).

    Is this a bug?

    Thursday, July 01, 2010 6:37 PM

All replies

  • If anyone is interested, I managed to work around this bug with some trouble by dynamically creating script elements in the DOM.  However, due to dependencies between the javascript files (in my real code, not the simple example above), I was required to use setTimeout and test for the existence of variables in the first javascript file before loading the second.

    I'm still really curious if IE developers know about this issue.  It sure seems like a bug to me (though admittedly, somewhat obscure).

    Tuesday, July 13, 2010 9:31 PM