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.
function openNew() {
var win = window.open('', 'new') ;
win.document.write('<html><head><title>New</title></head><body><div id="wrap">Hello World</div></body></html>') ;
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 ;
}
The document.write 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 document.write with the rest of the document but that didn’t help. Also tried using innerHTML instead of write() but that didn’t work at all, nor did creating the whole document (html, head and body tags) with createElement.
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.
Is this possible to do?
Trying closing the document, before accessing it.
Other option is to build up the string and just add it all at once and not append to it.