判断浏览器是否支持打印

发布于 2025-01-05 08:29:43 字数 291 浏览 1 评论 0原文

我认为这个问题的答案几乎肯定是“否”,因为我已经做了一些测试和搜索,但是有没有什么技巧可以检测 window.print() 甚至可能 从页面内部工作(即从 JavaScript)?我知道,即使在台式机/笔记本电脑上,也永远不可能知道系统上是否配置了打印机,但至少浏览器会弹出一个打印对话框。

我的 Android 手机有一个 window.print() 函数,但它(不出所料)没有执行任何操作。

再说一次,我问的主要是这样,所以在这个主题上有一个很好的问题:-)

I think the answer to this is almost certainly "no", because I've done a little testing and searching around, but is there any trick to detect whether window.print() even might work from inside a page (i.e., from JavaScript)? I know that even on a desktop/laptop it's never going to be possible to know whether there's a printer configured on the system, for example, but at least the browser will put up a print dialog.

My Android phone has a window.print() function but it (unsurprisingly) doesn't do anything.

Again I'm asking mostly so there's a good question on the topic at SO :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软糖 2025-01-12 08:29:43

不幸的是,它看起来像是没有。 window.print() 函数不是 EMCAScript 规范的一部分。这意味着它不需要成为 JavaScript 语言的一部分,也没有适当的实现文档。这是未定义的行为,因此测试它看起来非常困难。

来源:

编辑:

我为测试浏览器而编写的可爱的小脚本,只是检查打印函数是否存在,然后要求打印:

if(window.print) {
    if(confirm('I can print. Would you like to?'))
        window.print()
}

Unfortunately it looks like a no. The window.print() function is not part of the EMCAScript specification. This means that there's no requirement for it to be part of the JavaScript language, and no proper documentation for its implementation. It's undefined behaviour and so testing for it looks very difficult.

Sources:

EDIT:

Cute little script I wrote to test my browsers, just checks the print function exists and then asks to print:

if(window.print) {
    if(confirm('I can print. Would you like to?'))
        window.print()
}
尝蛊 2025-01-12 08:29:43

print() 方法是同步的。这使得可以进行后续处理以确定是否已显示打印对话框

var start = +new Date();
window.print();
var delta = + new Date() - start;
console.log(delta);
if (delta > 100) { console.log('It worked'); }

The print() method is synchronous. This makes it possible to do the aftermath in order to decide wether a print dialog has been shown

var start = +new Date();
window.print();
var delta = + new Date() - start;
console.log(delta);
if (delta > 100) { console.log('It worked'); }
∞琼窗梦回ˉ 2025-01-12 08:29:43

打印前和打印后事件可能会有所帮助,但我不确定浏览器是否支持。

编辑:Webkit 不支持它们

The beforeprint and afterprint events may help, but I'm not sure about browser support.

Edit: Webkit does not support them

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文