如何打印网页,而无需打开一个弹出窗口? [英] How to print a web page without opening a popup window?
问题描述
我要打印使用JavaScript的网页。但我不希望打开的页面作为一个弹出窗口。我怎么能直接打印网页一样使用JavaScript window.print方法,而无需打开它作为一个弹出窗口mypage.aspx?
另外的条件是:我不希望使用任何ActiveX这个
下面是我想尝试什么:
VAR printWindow,printData; printWindow = window.open(,列印,菜单栏,滚动条,宽度= 640,高度= 480,顶部= 0,左= 0);printData =的document.getElementById(lblReport)的innerHTML。 printWindow.document.write(printData);
printWindow.document.close();
printWindow.print();
该simpliest解决方案是该mypage.aspx的内容加载到一个iframe然后在onload事件调用window.print内部框架。
<按钮的onclick =的PrintPage()>打印< /按钮>
< DIV ID =printerDiv的风格=显示:无>< / DIV>
<脚本>
功能的PrintPage()
{
VAR的div =的document.getElementById(printerDiv);
div.innerHTML ='< IFRAME SRC =mypage.aspx的onload =this.contentWindow.print();>< / IFRAME>';
}
< / SCRIPT>
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>
这篇关于如何打印网页,而无需打开一个弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!