Search code examples
javascriptasp.netvb.netpopupwindow

Print window blocked by browser popup blocker


I'm trying to display a print dialog where a user can print out an invoice through a button control. The main portion of this code is javascript but I noticed that when I clicked the print button, (on 4 different pc's) the popup blocker in google chrome blocked the print page completely. After I allowed popups it worked fine but it is inconvenient for customers to have to constantly say allow popups. I've looked up another option where supposedly window.createPopup kind of bypasses the browser popup blocker but uses window restrictions. I also do not think the window.createPopup is a method in asp.net. How can I prevent the popup blocker from blocking the print page dialog when any customer from our website goes to print the page since I can't control their browser settings? Is it possible to do so?

<script type="text/javascript">
   function PrintGridData() {
       var prtGrid = document.getElementById('<%=panel22.ClientID %>');
       prtGrid.border = 0;
       var prtwin = window.open('', 'Printpanel22Data', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1, status=0,resizable=1');
       prtwin.document.write(prtGrid.outerHTML);
       prtwin.document.close();
       prtwin.focus();
       prtwin.print();
       prtwin.close();       
   }
    </script>

and the code behind to call the function

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    ClientScript.RegisterStartupScript(Me.[GetType](), "PrintOperation", "PrintGridData()", True)
End Sub

Solution

  • A long time ago I had to deal with something like this. I wound up abandoning the popup window to print and just used print styling in my CSS file. Yeah, it's a lot more work, and you're going to use a lot of paper to test it, but I had to cater to users who hated popups.

    The only other way to pop a window that I know of is a target="_blank" in the anchor tag...