检测平板电脑和手机、htaccess 或 CSS

发布于 2024-11-18 18:26:14 字数 408 浏览 4 评论 0原文

好吧,我已经研究这个有一段时间了,似乎有几种方法可以做到这一点,检测屏幕尺寸的 CSS 媒体查询、htaccess 和 javascript。

我更喜欢走 htaccess 路线,因为据我所知,平板电脑现在有 1024px 分辨率。这不会影响桌面分辨率吗? htaccess 还让我有机会为平板电脑和移动浏览器创建一个全新的网站,但我如何检测它是平板电脑还是手机呢?我不想检测它是 Google Nexus 还是 HTC Desire 或 iPhone,还是 iPad、Acer Iconia、Xoom 等。

有没有一种方法可以检测平板电脑、手机或台式电脑,而不必包括每个品牌和型号? (例如此处。)

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 mobile 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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无名指的心愿 2024-11-25 18:26:14

我建议使用 Modernizr 来实现此目的。

它使用客户端浏览器上的 Javascript 来检测对所有浏览器功能(包括触摸事件)的支持,并为您提供 CSS 类和 Javascript 对象,您可以使用它们来调整站点以适应用户的浏览器。

我总是避免在服务器上进行浏览器检测(即在 .htaccess 或服务器端代码中),因为它不可靠——服务器可以用来检测客户端环境的数据以纯文本形式发送,很容易被欺骗或被黑客入侵。更重要的是,它通常也受到代理和隐私应用程序的抑制。

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.

小兔几 2024-11-25 18:26:14

如果您想使用 .htaccess 检测移动浏览器,则必须列出它们的名称(至少是最独特的部分)。例如,可以使用单个重写规则来完成(取自关于 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]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文