首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从Javascript调用打印预览?

如何从Javascript调用打印预览?
EN

Stack Overflow用户
提问于 2008-10-23 15:33:58
回答 3查看 155.4K关注 0票数 63

我有一个页面,它应该在加载时启动打印预览页面。

我发现了这个:

var OLECMDID = 7;
/* OLECMDID values:
* 6 - print
* 7 - print preview
* 1 - open window
* 4 - Save As
*/
var PROMPT = 1; // 2 DONTPROMPTUSER
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
WebBrowser1.ExecWB(OLECMDID, PROMPT);
WebBrowser1.outerHTML = "";

但是..。

  1. 它在FireFox中不起作用。
  2. 它有点丑陋。

有没有更好的IE或者FireFox的方法呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2008-10-23 15:38:39

你不能,打印预览是浏览器的一项功能,因此应该防止被JavaScript调用,因为这将是一个安全风险。

这就是您的示例使用Active X的原因,它绕过了JavaScript安全问题。

因此,不要使用media=print,而要使用您已经拥有的打印样式表,并将其显示在media=screen中。

阅读Alist Apart: Going to Print,可以找到一篇关于打印样式表的好文章。

票数 35
EN

Stack Overflow用户

发布于 2008-10-23 15:40:28

我认为在跨浏览器JavaScript中最好的是window.print(),它(对我来说,在Firefox3中)会弹出‘打印’对话框,而不是打印预览对话框。

仅供参考,打印对话框是您的计算机的打印弹出窗口,当您按Ctrl-p组合键时,您将看到该对话框。打印预览是Firefox自己的预览窗口,它有更多的选项。这就是你使用Firefox菜单>打印...

票数 29
EN

Stack Overflow用户

发布于 2014-05-21 13:19:25

这可以使用javascript来完成。假设您的html/aspx代码是这样的:

<span>Main heading</span>
<asp:Label ID="lbl1" runat="server" Text="Contents"></asp:Label>
<asp:Label Text="Contractor Name" ID="lblCont" runat="server"></asp:Label>
<div id="forPrintPreview">
  <asp:Label Text="Company Name" runat="server"></asp:Label>
  <asp:GridView runat="server">

      //GridView Content goes here

  </asp:GridView
</div>

<input type="button" onclick="PrintPreview();" value="Print Preview" />

单击"Print Preview“按钮,我们将打开一个包含要打印的数据的窗口。注意到'forPrintPreview‘是一个div的id。打印预览的功能是这样的:

function PrintPreview() {
 var Contractor= $('span[id*="lblCont"]').html();
 printWindow = window.open("", "", "location=1,status=1,scrollbars=1,width=650,height=600");
 printWindow.document.write('<html><head>');
 printWindow.document.write('<style type="text/css">@media print{.no-print, .no-print *{display: none !important;}</style>');
 printWindow.document.write('</head><body>');
 printWindow.document.write('<div style="width:100%;text-align:right">');

  //Print and cancel button
 printWindow.document.write('<input type="button" id="btnPrint" value="Print" class="no-print" style="width:100px" onclick="window.print()" />');
 printWindow.document.write('<input type="button" id="btnCancel" value="Cancel" class="no-print"  style="width:100px" onclick="window.close()" />');

 printWindow.document.write('</div>');

 //You can include any data this way.
 printWindow.document.write('<table><tr><td>Contractor name:'+ Contractor +'</td></tr>you can include any info here</table');

 printWindow.document.write(document.getElementById('forPrintPreview').innerHTML);
 //here 'forPrintPreview' is the id of the 'div' in current page(aspx).
 printWindow.document.write('</body></html>');
 printWindow.document.close();
 printWindow.focus();
}

注意到' print‘和'cancel’按钮的css类是'no-print',所以这些按钮不会出现在打印中。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/230205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档