Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a web page without opening a popup window?

I want to print a web page using JavaScript. But I do not want to open the page as a popup windows. How can I print directly a web page like 'mypage.aspx' using JavaScript window.print method without opening it as a popup window?

Also the condition is 'I don't want to use any ActiveX for this'

Here is what I want to try:

var printWindow, printData; printWindow = window.open("", "printVersion", "menubar,scrollbars,width=640,height=480,top=0,left=0");

printData=document.getElementById("lblReport").innerHTML; printWindow.document.write(printData); 
printWindow.document.close(); 
printWindow.print();  
like image 674
Gaurav Arora Avatar asked Apr 16 '10 08:04

Gaurav Arora


People also ask

How can I print Javascript without displaying the Print dialog box?

Simply assign the desired printer as the default printer in Windows and make sure you're using the Chrome Engine under the browser settings (the default setting).


1 Answers

The simpliest solution is to load the content of that mypage.aspx to an iframe then on iframes onload event call the window.print.

<button onclick="printPage()">print</button>
<div id="printerDiv" style="display:none"></div>
<script>
   function printPage()
   {
      var div = document.getElementById("printerDiv");
      div.innerHTML = '<iframe src="mypage.aspx" onload="this.contentWindow.print();"></iframe>';
   }
</script>
like image 68
jerjer Avatar answered Oct 04 '22 00:10

jerjer