Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if user has printing capabilities?

A client has asked for a print button be added to their site and would like it to be hidden for users who don't have the capability to print, e.g. most mobile devices.

Is there any way through JavaScript to detect if a client has printing capabilities?

like image 274
JohnC Avatar asked Aug 31 '12 13:08

JohnC


1 Answers

The requirement is flawed since most user agents can "print" and the knowledge of whether or not a UA can print is not the websites busines.

Many mobile browsers can print and most web browsers can print even if there is no printer attached (print to pdf, cloud print etc). It is a bit of a security problem for any user agent to explicitly state anything about its printing capabilities without the user's knowledge. That is what the printing stylesheet is there for (so the website doesn't have to know if it is being printed at all).

What you can do is hide the button on user agents with small screens, those users can still print their documents using the user agent itself. You could also detect specific user agents and hide the button for them.

Links

http://www.alistapart.com/articles/return-of-the-mobile-stylesheet : discusses mobile stylesheets and related issues.

http://mobile.smashingmagazine.com/2010/11/03/how-to-build-a-mobile-website/#mobile-stylesheets : more about mobile stylesheets.

Basic mobile stylesheet attachment:

<link rel="stylesheet" href="mobile.css" media="handheld" />

Detecting by screen size:

<link rel="stylesheet" href="mobile.css" media="only screen and (max-device width:480px)"/>

like image 176
jmh Avatar answered Nov 19 '22 06:11

jmh