Print a Web Page Using JavaScript

Thường thì hàm này không lấy được css trên fifefox window.print not working in firefox

Recently I was implementing a functionality where we upload a file to server, retrieve its data then show summary based on it. I was showing this summary in a panel with a print button. This print button uses JavaScript function window.print () to print the summary report from the contents of the panel. Here is the function that I was using –

1
2
3
4
5
6
7
8
9
function CallPrint() {
    var prtContent = document.getElementById('<%= pnlSummary.ClientID %>');
    var winPrint = window.open('', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
    winPrint.document.write("<h3>Summary</h3>" + prtContent.innerHTML);
    winPrint.document.close();
    winPrint.focus();
    winPrint.print();
    winPrint.close();
}

Everything was working fine until it came to Firefox. In Firefox the print dialog was not getting opened in the new window. To resolve this I changed the calling mechanism of the print function, I added a new page to project solution Print.aspx. Instead of opening a new window without any URL, I opened this page in the window and accessed the contents of the pnlSummary from the opened window via window.opener object –

1
2
3
function CallPrint() {
    var winPrint = window.open('Print.aspx', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
}

On Print.aspx page I used this function –

1
2
3
4
5
6
7
function Print() {
    var prtContent = "<h3>Summary</h3>" + window.opener.document.getElementById('ctl00_cphContent_pnlSummary').innerHTML;
    document.getElementById("printDiv").innerHTML = prtContent;
    window.print();
    window.opener.focus();
    window.close();
}

and called it on body onload.

1
2
3
4
5
6
<body onload="Print();">
    <form id="form1" runat="server">
    <div id="printDiv">
    </div>
    </form>
</body>

nguồn :http://utkarshpuranik.com/

Các trình duyệt khác đều sử dụng được trừ fifefox

<A HREF=”javascript:window.print()”>Click to Print This Page</A>

<A HREF=”javascript:window.print()”>
<IMG SRC=”print_image.gif” BORDER=”0″</A>

<SCRIPT LANGUAGE="JavaScript"> 
if (window.print) {
document.write('<form><input type=button name=print value="Print" onClick="window.print()"></form>');
}
</script>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s