Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine whether browser supports printing

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

like image 263
Pointy Avatar asked Feb 13 '12 22:02

Pointy


People also ask

How do you print from a website that won't let you?

Some websites prevent you from using keyboard shortcuts because they are worried that you will steal content such as trying to save an image by right-clicking on it. Try using the browser's "Print" option in the top menu, instead of Ctrl + P or the right-click menu.

How do you find whether a browser supports JavaScript or not?

To find whether the browser supports JavaScript or nor, use the <noscript> tag. The HTML <noscript> tag is used to handle the browsers, which do recognize <script> tag but do not support scripting. This tag is used to display an alternate text message.

What is browser print function?

print() Opens the print dialog to print the current document. If the document is still loading when this function is called, then the document will finish loading before opening the print dialog. This method will block while the print dialog is open.


2 Answers

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.org/en/DOM/window.print
  • http://www.ecma-international.org/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()
}
like image 83
Jivings Avatar answered Sep 21 '22 06:09

Jivings


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'); }
like image 39
user123444555621 Avatar answered Sep 18 '22 06:09

user123444555621