最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Determine whether browser supports printing - Stack Overflow

programmeradmin4浏览0评论

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 :-)

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 :-)

Share Improve this question asked Feb 13, 2012 at 22:13 PointyPointy 414k62 gold badges594 silver badges626 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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:

  • https://developer.mozilla/en/DOM/window.print
  • http://www.ecma-international/publications/files/ECMA-ST/Ecma-262.pdf

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()
}

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'); }

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

Edit: Webkit does not support them

发布评论

评论列表(0)

  1. 暂无评论