9

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?

JohnC
  • 1,377
  • 11
  • 33
  • 10
    In a word, no. The browser doesn't get to know anything about attached devices. You can certainly tell if the client is mobile, and hide the button for mobile devices, but you cannot tell anything about the presence printers. – user229044 Aug 31 '12 at 13:41
  • 2
    Is this for a public internet site (in which case it's impossible) or for a private intranet site? If it's for an intranet site, can you require a specific browser, and can you install plugins, e.g. ActiveX? – RB. Aug 31 '12 at 13:43
  • 1
    ... and knowing that a device is "mobile" is pretty hard too, as the number of device species in the wild is growing so fast lately. – Pointy Aug 31 '12 at 13:43
  • @RB ActiveX is probably not super-useful for most mobile devices ... – Pointy Aug 31 '12 at 13:44
  • 6
    And, a number of mobile devices *can* print, especially printing to a file! – RB. Aug 31 '12 at 13:45
  • To follow up from RB's comment, you could potentially try to request something from the Printers IP and see what the response is, crazy though. – Dunhamzzz Aug 31 '12 at 14:06
  • Unfortunately its a public site, I guessed it wouldn't be possible but thought i'd double check on stack – JohnC Aug 31 '12 at 14:11
  • Yeah you can detect a lot of mobile devices using WURFL as described here http://stackoverflow.com/a/3632172/1354137, but that looks like your only option. – alexvance Aug 31 '12 at 14:35
  • 3
    "e.g. most mobile devices." Umm...Airprint? Most mobile devices *CAN* print. This is a bad idea. – aquinas Aug 31 '12 at 15:54

3 Answers3

1

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)"/>

jmh
  • 8,856
  • 1
  • 18
  • 26
0

If the WURFL regex is too slow for your Application or you are using varnish, squid or anything else that doesn't allow you to make use of WURFL, you might just try to analyze the user agent string with JS (navigator.userAgent) and find out something like "iOS version 4+" as at least those devices have printing capabilities (as long as they are in reach of a network with a network printer). This is a simple solution (but will never cover all iOS devices as they have too many different user agent strings).

You can find plenty of examples for user agent strings over here: http://deviceatlas.com/

Achim Koellner
  • 913
  • 12
  • 22
-1

Couple of clarifications. WURFL does not use RegExps, or at least, not in the way that the comment seems to imply. RegExps may be involved for some UAs that are particular hard to analyze, but this only happens once for each UA, after that the match is cached.

Also, ScientiaMobile recently announce the availability of a WURFL module for Varnish Cache, Apache and NGINX, so using WURFL at the "network" level is now possible.

This page has more details: http://www.scientiamobile.com/blog/post/view/id/25/title/HTTP-and-Mobile%3A-The-Missing-Header-

finally a disclaimer: I know these things because I am the WURFL creator and ScientiaMobile's CTO.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • 1
    This isn't really an answer to the question; I think you meant to add this as a comment on the answer that mentions WURFL. – millimoose Oct 08 '12 at 00:17