检测网络上的移动浏览器?

发布于 2024-09-17 13:22:26 字数 446 浏览 3 评论 0原文

我很想知道如何检查 iPhone、iPad 和其他移动浏览器。(JavaScript 或 CSS)

编辑:

请不要使用用户代理字符串。那是可以伪造的。

可能的欺骗:

I'm curious to know how to check for iPhone, iPad and other mobile browsers.(JavaScript or CSS)

Edit:

Not user agent string, please. That can be faked.

Possible Dupes:

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

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

发布评论

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

评论(4

夜空下最亮的亮点 2024-09-24 13:23:47

使用 javascript 检测 HTTP 用户代理 - 浏览器名称的奇特术语

Use javascript to detect the HTTP User Agent - fancy term for name of the browser

浊酒尽余欢 2024-09-24 13:23:35

使用 Modernizr - http://modernizr.com/

Modernizr 是一个 JS 脚本,用于测试浏览器的各种 HTML5 和页面加载时的 CSS3 功能。您可以查看 Modernizr JS 对象,或者使用它添加到 HTML 元素的类。如果存在“touch”类,则您有一个触摸屏设备;否则,该类是无接触的。然后您可以在 CSS 中执行此操作

.touch .myElement { /* touch device styles */ }

.no-touch .myElement { /* regular browser styles */ }

。测试浏览器功能比嗅探用户代理更有用且面向未来。通过这种方式,对于您想要添加的每个 CSS3 功能,您可以轻松地编写一个后备,如我在此处所示。

Use Modernizr - http://modernizr.com/

Modernizr is a JS script that tests the browser for a variety of HTML5 and CSS3 capabilities when the page loads. You can either look in the Modernizr JS object, or use the classes it adds to the HTML element. If the class 'touch' is present, you have a touchscreen device; otherwise, the class is no-touch. Then you can do this in your CSS

.touch .myElement { /* touch device styles */ }

.no-touch .myElement { /* regular browser styles */ }

Testing a browsers capabilities is far more useful and future-proof that sniffing for user agents. In this way, for each CSS3 feature you want to add, you can easily write a fallback, as I show here.

素食主义者 2024-09-24 13:23:23

基本上,您检查用户代理字符串,

请参阅 http://www.hand- Interactive.com/resources/detect-mobile-javascript.htm

检测 iPhone:

navigator.userAgent.toLowerCase().search("iphone") > -1

一般来说,功能检测比浏览器检测更好,了解用户的浏览器可以做什么比了解用户正在使用什么更好。 Modernizer 是一个很好的工具。

Basically you check the User Agent String

see http://www.hand-interactive.com/resources/detect-mobile-javascript.htm

Detect iPhone:

navigator.userAgent.toLowerCase().search("iphone") > -1

In general feature detection is better than browser detection it is better to know what the user's browser can do than what he's using. Modernizer is a good tool for that.

最冷一天 2024-09-24 13:23:10

我会使用 WURFL。它是一个包含 10000 多个移动设备的开源 xml 数据库,根据 user-agent HTTP 标头值。

您将获得如下信息:

  1. 屏幕尺寸
  2. XHTML/HTML 支持级别
  3. 图形类型支持

许多其他信息。

PHP、Java 和 .NET 等流行语言都有包装 API,因此您不必自己处理 XML 数据库。

I would use WURFL. It's an Open Source xml-database of more than 10000 mobile devices that will detect (almost always) your Mobile Phone and Browser capabilities given the user-agent HTTP header value.

You will get information like:

  1. Screen size
  2. XHTML/HTML support level
  3. Graphic type support

Many others.

There are wrapper APIs for popular languages such as PHP, Java and .NET, so you won't have to deal with the XML database yourself.

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