tags:

views:

258

answers:

2

Hello gurus :)

I have been struggling with this for a while now, and decided it was time to ask for help.

I am trying to create a print function on an aspx page, and I am using javascript to do this:

        function PrintContentCell() {
        var display_setting = "toolbar=yes, location=no, directories=yes, menubar=yes,";
        display_setting += "scrollbars=yes, width=750, height=600, left=100, top=25";

        var content_innerhtml = document.getElementById("testTable").innerHTML;

        var document_print = window.open("Loading.htm", "", display_setting);
        document_print.document.open();
        document_print.document.write('<html><head><title>Print</title></head>');
        document_print.document.write('<body style="font-family:verdana; font-size:12px;" onLoad="self.print();self.close();" >');
        document_print.document.write(content_innerhtml);
        document_print.document.write('</body></html>');
        document_print.print();
        document_print.document.close();
        return false;
    }

I get "Access Denied" when the script tries to write to the new window. The Loading.htm file is just a very slim html document writing out the text "Loading...". I had hoped this would work after reading this thread: http://stackoverflow.com/questions/735136/ie-6-7-access-denied-trying-to-access-a-popup-window-document

Anybody can help?

A: 

Could you not simply .write() the "Loading" markup after creating an empty (window.open("", ...)) popup?

It would avoid a trip to the server, seem more responsive to the user and solve your problem.

Edit In-fact, as your just shuffling data about on the client side, does the time it takes to render the HTML really warrant a loading banner?

Alex K.
I tried that at first, but that gave me the "Access Denied" error on write(). Therefore I tried with using a static html loading page, to see if that helped - but unfortunately it didn't :(
Søren
+1  A: 

If you want a new, empty popup window to write into, the usual approach is use an empty string for the URL:

window.open('', '_blank', features)

There's no point trying to open HTML from the server when you're immediately going to replace all the content before it's even had a chance to load. (Your empty window name "" may also cause problems.)

However, this is in any case not a good way to implement a “print version” of a page. Instead, use a print stylesheet which hides all but the contents of testTable.

bobince
Hi bobince. Thanks a lot for your answer. The reason I have the Loading.htm is because I got the Access Denied as well without it, and to quote the StackOverflow user David from this thread http://stackoverflow.com/questions/735136/ie-6-7-access-denied-trying-to-access-a-popup-window-document: "IE considers "about:blank" to be a insecure URL and it won't let you talk to it. I would create a "Now Loading..." static HTML file and open that instead."
Søren
I still get the Access Denied error, also when I provide no loading html and the '_blank' target :(I will try reading your suggestion on using css for printing, and see if that works for me
Søren
Works for me. `''` is a different thing to `'about:blank'`. You're not setting `document.domain` on the parent window are you?
bobince