如何在不打开弹出窗口的情况下打印网页?

Gau*_*raa 11 javascript asp.net

我想使用JavaScript打印网页.但我不想打开页面作为弹出窗口.如何使用JavaScript window.print方法直接打印像'mypage.aspx'这样的网页而不将其打开为弹出窗口?

条件是'我不想为此使用任何ActiveX'

这是我想要尝试的:

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();  
Run Code Online (Sandbox Code Playgroud)

jer*_*jer 22

最简单的解决方案是将mypage.aspx的内容加载到iframe,然后在iframes onload事件上调用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>
Run Code Online (Sandbox Code Playgroud)


Ros*_*sim 6

<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>
Run Code Online (Sandbox Code Playgroud)

确保all.css在顶部和print.css底部,它应该工作.

  • Gaurav,你得到了一个志愿者社区的免费帮助.责骂他们寻找你不喜欢的答案并不是鼓励他们花时间帮助你的好方法...... (5认同)