如何从 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,print 而不是 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屋!