简体   繁体   中英

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();  

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>
<html>
    <head>
        <title>Print</title>
        <link rel="stylesheet" type="text/css" media="all" href="all.css" />
        <link rel="stylesheet" type="text/css" media="print" href="print.css" />
    </head>
<body>
    <p>I get printed</p>
    <form>
        <input type="button" onclick="window.print()" value="Print" />
    </form>
</body>
</html>

Make sure all.css is on top and print.css at the bottom, it should work.

不确定它是否有效,但您可以尝试创建不可见的iframe ,在其中加载page2.aspx然后打印它。

Um. Just use window.print(); directly on the page. By itself it does not open a new window (apart from the print properties window to set printing options).

You could just use a CSS print style sheet and avoid using javascript altogether -all the user has to do them is print the page. eg

<link rel="stylesheet" type="text/css" media="print" href="print.css" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM