Dynamically create entire page, including scripts and styles
<p>I'm playing around with the following code. The intention is to open a new window and create a basic document from scratch, rather than loading it from file. </p>

<pre><code>function openNew() {
    var win = window.open('', 'new') ;

    win.document.write('&lt;html&gt;&lt;head&gt;&lt;title&gt;New&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;div id="wrap"&gt;Hello World&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;') ;

    var head = win.document.getElementsByTagName('head')[0] ;

    var style = win.document.createElement('link') ;
    style.rel = "stylesheet" ;
    style.type = "text/css" ;
    style.href = "/css/main.css" ;
    head.appendChild(style) ;

    var script = win.document.createElement('script') ;
    script.type = "text/javascript" ;
    script.src = "/js/main.js" ;
    head.appendChild(script) ;

    return false ;
}
</code></pre>

<p>The <code>document.write</code> part seems to work, but the rest does nothing. Or at least nothing that I can tell, I get no errors or anything. I tried to just write the script part in the <code>document.write</code> with the rest of the document but that didn't help. Also tried using <code>innerHTML</code> instead of <code>write()</code> but that didn't work at all, nor did creating the whole document (html, head and body tags) with <code>createElement</code>.</p>

<p>If it has any importance, the script that needs to be loaded is the same one that does the opening in the first place. I don't know if that somehow creates a conflict though I'm not sure why it would do that. Just mentioning it for good measure.</p>

<p>Is this possible to do?</p>
