开发者

Detecting tablets and phones, htaccess or CSS

Ok so I've been researching this for a while and seems there are a few ways to do this, CSS media queries that detect the screen size, htaccess and javascript.

I would prefer to go the htaccess route, because as far as I know, tablets now have 1024px resolution. Wouldn't this interfere with desktop resolutions? Also htaccess gives me the opportunity to make a whole new site for tablets and mobi开发者_运维百科le browsers, but how do I go about detecting whether it's a tablet or a mobile? I don't want to have to detect if it's a Google Nexus vs an HTC Desire or iPhone, up against iPad, Acer Iconia, Xoom, etc.

Is there a way to detect a tablet, phone or desktop computer, without having to include every single make and model? (for example as here.)


I would suggest using Modernizr for this.

It uses Javascript on the client browser to detect support for a whole range of browser features, including touch events, and gives you CSS classes and a Javascript object which you can use to adjust your site to suit the user's browser.

I would always avoid doing browser detection on the server (ie in .htaccess or in your server-side code), because it's unreliable -- the data which the server can use to detect the client environment is sent as plain text and is easily spoofed or hacked. More importantly, it's also commonly suppressed by proxies and privacy apps.


If you want to detect mobile browser using .htaccess, you would have to list their names (at least most unique part). It can be done with a single rewrite rule, for example (taken from another question on SO):

RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{REQUEST_URI} !^/mobile.php
RewriteRule .* /mobile.php?url=%{REQUEST_URI} [L]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜