0

I'm trying to open a new window with javascript and fill it with the html from the current window. Sort of like a print preview in a new window and with a new css.

function prnt() {
        var pr = window.open("", "", "width=1020,height=750");
        pr.document.open();
        pr.document.write('<html><head><title>Test</title>');
        pr.document.write('<link href=print.css rel=Stylesheet></head><body>');
        pr.document.write(document.body.innerHTML);
        pr.document.write('</body></html>');
        pr.document.close();
        pr.focus();}  

I'm calling this function from here:

<a id="print" href="#" runat="server" onclick="javascript:prnt();">PRINT</a>  

IE gives me the error the remote procedure call failed and it seems to be pointing to this line:
pr.document.close();

Everything works fine in FF though. Any ideas?

EDIT
I'm running on a Windows Server 2008 R2 Standard
SP1

EDIT2
If I remove pr.document.close(); the page works perfect in IE and done is displayed in the status bar.
However, now FF won't work properly. The page never actually stops loading. I guess it's waiting for the pr.document.close(); command?

Niklas
  • 12,276
  • 21
  • 71
  • 114

2 Answers2

3

Use the DOM...document.write is evil.

On how to do it check here:

Community
  • 1
  • 1
Luis
  • 5,833
  • 2
  • 29
  • 51
  • In essence, you're right, but not when writing to a new window: "I think you will probably have to use at least some document.write(), to put a skeleton page in place before you can interact with the DOM" ([read on](http://stackoverflow.com/questions/265398/refactoring-a-function-that-uses-window-open-to-use-the-dom-rather-than-write/541456#541456)). – Marcel Korpel Apr 07 '11 at 13:12
  • If you can supply a working javascript wich uses the DOM I'll mark this as the best answer =) – Niklas Apr 07 '11 at 13:15
1

It's not as beautiful as it is an easy solution, but it seem to be working.
I replaced this line of code:

pr.document.close();  

with this line:

if (navigator.appName != "Microsoft Internet Explorer") pr.document.close();
Niklas
  • 12,276
  • 21
  • 71
  • 114
  • Of course this is an ugly hack, but it does not solve your *actual* problem: e.g., I also used `document.write` and `document.close` to populate a new window, but I never got this error in IE. – Marcel Korpel Apr 07 '11 at 13:51