views:

277

answers:

3

I have some Javascript that is opening a blank window, assigning it with a stylesheet and then writing some text to it. This is all working fine except that the content is not having the styles applied to it.

The code looks like this:

var newWindow = window.open('', 'SecondWindow', 'toolbar=0,stat=0');
var style = newWindow.document.createElement('link');
style.type = "text/css";
style.rel = "stylesheet";
style.href = "styles/style.css"; 
newWindow.document.getElementsByTagName("head")[0].appendChild(style);
newWindow.document.body.innerHTML="<p class='verystylish'>Hello world!</p>";

If I use the Firefox Web Developer tools to view the generated source, save that as an html file and then open the html file manually, it applies the styles correctly, so it looks as though I need to be doing something to force the browser to apply the styles or re-render the page somehow. Any suggestions?

Edited to add, the generated source looks like this:

<html>
  <head>
    <title></title>
    <link href="styles/style.css" rel="stylesheet" type="text/css">
  <head>
  <body>
    <p class='verystylish'>Hello world!</p>
  </body>
</html>

The problem being that no style whatsoever is assigned to the paragraph. But opening a file with the same source code renders correctly.

+1  A: 

What do you mean by "it applies the styles correctly"? Are you talking about the class="verystylish"? If that's it, then it doesn't mean styles are getting applied at all, but that the new paragraph element has that class.

You're not writing anything to the window except for that <P>. Don't you have to write the whole code of the page, including <HTML>, <HEAD> and <BODY> tags?

Seb
Hopefully the edit clarifies. On the generated page the style is not applied, the same source code as an html document shows the style correctly. Window.open with a blank filename ( in firefox at least ) opens a window containing an empty html document.
glenatron
+1  A: 

Firefox doesn't set up the base URL correctly, so the relative link /styles/styles.css isn't pointing anywhere (or at least, that's the impression I got by generating an < a> element into the new window and then asking the respective browsers to copy the link location into the clipboard).

Which would imply it might work if you load an empty html document (rather than about:blank) that's got the correct base URL to work from. I've had to do something similar in the past (keep a copy of about:blank available), because I was getting a warning about mixed secure/unsecured items being shown.

Damien_The_Unbeliever
+2  A: 

A new window opens by default with the URL ‘about:blank’. Relative URL references won't work for this reason. To change the location of the window to match the original document, call:

newWindow.document.open();

You should then write the skeleton of the document you want to the window. You can include the stylesheet now if you want, as document.write is a more reliable way of adding a stylesheet across older browsers than DOM methods. This method also allows you to write a DOCTYPE so you don't unexpectedly end up in Quirks Mode.

newWindow.document.write(
    '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;'+
    '<html><head>'+
    '  <link rel="stylesheet" href="styles/style.css">'+
    '</head><body>'+
    '</body></html>'+
);
newWindow.document.close();
bobince