Detect if user has printing capabilities?

爷,独闯天下 提交于 2019-12-21 09:21:46

问题


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?


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




回答2:


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/




回答3:


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.



来源:https://stackoverflow.com/questions/12216551/detect-if-user-has-printing-capabilities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!