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 –

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 –

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 –

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.

<body onload="Print();">
    <form id="form1" runat="server">
    <div id="printDiv">
    </div>
    </form>
</body>
About these ads

About Utkarsh Puranik

Software Engineer by Profession, Gamer by Nature, Techy by Attitude and a Good Person at Heart
This entry was posted in Technology and tagged , , . Bookmark the permalink.

5 Responses to window.print not working in firefox

  1. snehal says:

    Hi utkarsh,

    this blog helped me a lot.

    Thanks yar.

  2. Agustin says:

    Hi, i would like to know, how could I do it in PHP??

    • Utkarsh Puranik says:

      Hi,
      I am not a php guy so can’t tell you much about that but I don’t think these javascript snippets will differ in php.

  3. Pingback: Print a Web Page Using JavaScript | Front End Developer

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