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

标签 asp.net javascript

我想使用 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();  

最佳答案

最简单的解决方案是将 mypage.aspx 的内容加载到 iframe,然后在 iframe 的 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>

关于asp.net - 如何在不打开弹出窗口的情况下打印网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2651559/

相关文章:

asp.net - ObjectDataSource 的意义是什么

javascript - 在 UpdatePanel 回发时立即执行 Javascript

javascript - ExtJs 中的反向 RowNumberer?

javascript - 这是什么通信 : my_var = my_var || 69

javascript - 将鼠标悬停在 div 上可以平滑地更改背景

javascript - Jquery Onclick 优先级与 Href

javascript - 防止 Elements Below 弹出窗口响应

asp.net - 在 ASP.NET 中将 MS Word 文档转换为 PDF

sql - 在 SQL 中将简单的 XML 转换为表

asp.net - 如何阻止来自同一 IP 的多个请求