8

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

Pointy
  • 405,095
  • 59
  • 585
  • 614

3 Answers3

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:

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()
}
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Jivings
  • 22,834
  • 6
  • 60
  • 101
  • Yes that's what I had pretty much decided. Well maybe now this will serve as a good point of reference too :-) – Pointy Feb 13 '12 at 22:52
  • @Pointy Could perhaps make this community wiki and suggest people add any browsers that don't support the print function? – Jivings Feb 13 '12 at 23:16
  • Thanks for the fiddle, but it shows that your script doesn't work. At least with my device (with windows phone 8) it shows the confirm dialog but it does nothing if I accept. – Fernando Feb 05 '14 at 10:38
  • 1
    @Fernando This was a long time ago. Yet I'm fairly sure the point of this answer was to prove that it wont work. – Jivings Feb 05 '14 at 16:47
  • Sorry Jivings, you are right, I thought it was a script to test if browser can print, but it is a script to test if you can use window.print to test if browser can print, am I right? – Fernando Feb 06 '14 at 10:17
  • For me this works (at least the part which checks if browser support printing): if(window.print()) { – Nenad Bulatović Sep 01 '15 at 12:33
5

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'); }
user123444555621
  • 148,182
  • 27
  • 114
  • 126
  • Although user must try to print to test if he can print :). Anyway you can use this script and show a message if printing fails (not delta > 100). – Fernando Feb 05 '14 at 11:12
  • I'm assuming that if a browser defines window.print (to avoid scripts breaking that try to call it) but do not actually do anything that it would take less than 100ms to process the next line of code. I like it. I just wish that there were a way to test w/o actually calling it! This is the best solution I've seen yet, though. – jinglesthula Feb 13 '14 at 20:50
  • 1
    BTW, in case anyone is wondering about that plus sign, http://stackoverflow.com/questions/221539/what-does-the-plus-sign-do-in-return-new-date – jinglesthula Feb 13 '14 at 22:04
  • 1
    I tested with iPhone 3G running iOS 4.2.1, 4S running iOS 7, 5S running iOS 7, and Galaxy Note 2 running who knows what. The 4S hit 79-99ms range, the 5S 25-26ms range, the 3G 2-3ms range (no printing support), and the Note in the 0-3ms range (had printing support). On the Note at least it appears that print() is asynchronous :( boo. The best we could do is come up with appropriate flash message copy and hope the android users didn't notice it because it would fade out before their print dialog was closed. – jinglesthula Feb 13 '14 at 22:49
3

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

Edit: Webkit does not support them

user123444555621
  • 148,182
  • 27
  • 114
  • 126