I have been struggling with this for a while now, and decided it was 开发者_StackOverflow社区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: IE 6/7 Access Denied trying to access a popup window.document
Anybody can help?
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
.
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?
精彩评论