如何从Javascript调用打印预览? [英] How can Print Preview be called from Javascript?
问题描述
我有一个应该启动打印预览页面的页面。
I have a page that is supposed to launch the Print Preview page onload.
我发现了这个:
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 = "";
但是......
- 它在FireFox中不起作用。
- 它有点难看。
对于IE或对FireFox有效的方法有更好的方法吗?
Is there a better way for IE or a way that works for FireFox?
推荐答案
你不能,打印预览是一个浏览器的功能,因此应该受到保护而不被JavaScript调用,因为这会带来安全风险。
You can't, Print Preview is a feature of a browser, and therefore should be protected from being called by JavaScript as it would be a security risk.
这就是为什么你的例子使用Active X,它会绕过JavaScript安全问题。
That's why your example uses Active X, which bypasses the JavaScript security issues.
因此,请使用您应该拥有的打印样式表并将其显示为media = screen,而不是media = print。
So instead use the print stylesheet that you already should have and show it for media=screen,print instead of media=print.
阅读 Alist Apart:Going to Print 获取一篇好文章关于打印样式表的主题。
Read Alist Apart: Going to Print for a good article on the subject of print stylesheets.
这篇关于如何从Javascript调用打印预览?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!