使用 jQuery 检测 iPad 用户?
有没有办法使用 jQuery/JavaScript 来检测当前用户是否正在使用 iPad?
Is there a way to detect if the current user is using an iPad using jQuery/JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
iPad 检测
您应该能够通过查看
userAgent
属性:iPhone/iPod 检测
同样,
userAgent
属性:iPhone/iPod 检测 org/en-US/docs/Web/API/NavigatorID/platform" rel="noreferrer">platform
属性来检查 iPhone 或 iPod 等设备:注释
,但您通常应该避免执行特定于浏览器的检测,因为它通常不可靠(并且可能被欺骗)。最好使用实际的功能检测< /a> 在大多数情况下,这可以通过像 Modernizr 这样的库来完成。
正如 Brennen 的回答中指出的,在 Facebook 应用程序中执行此检测时可能会出现问题。请参阅他处理这种情况的答案。
相关资源
iPad Detection
You should be able to detect an iPad user by taking a look at the
userAgent
property:iPhone/iPod Detection
Similarly, the
platform
property to check for devices like iPhones or iPods:Notes
While it works, you should generally avoid performing browser-specific detection as it can often be unreliable (and can be spoofed). It's preferred to use actual feature-detection in most cases, which can be done through a library like Modernizr.
As pointed out in Brennen's answer, issues can arise when performing this detection within the Facebook app. Please see his answer for handling this scenario.
Related Resources
尽管接受的解决方案对于 iPhone 来说是正确的,但它会错误地声明
isiPhone
和isiPad
对于通过 Facebook 应用在 iPad 上访问您网站的用户来说是正确的。传统观点认为 iOS 设备有一个 Safari 用户代理和一个 UIWebView 用户代理。这种假设是不正确的,因为 iOS 应用程序可以并且确实自定义其用户代理。这里的主要罪犯是Facebook。
比较 iOS 设备中的这些用户代理字符串:
请注意,在 iPad 上,Facebook UIWebView 的用户代理字符串包括“iPhone”。
在 JavaScript 中识别 iPhone / iPad 的旧方法:
如果您采用这种方法来检测 iPhone 和 iPad,那么如果用户来自 Facebook,您最终会得到 IS_IPHONE 和 IS_IPAD 均为 true一个iPad。这可能会产生一些奇怪的行为!
在 JavaScript 中识别 iPhone / iPad 的正确方法:
我们在 iPad 上声明 IS_IPHONE 为 false,以掩盖奇怪的 Facebook UIWebView iPad 用户代理。这是用户代理嗅探如何不可靠的一个例子。自定义用户代理的 iOS 应用程序越多,用户代理嗅探的问题就越多。如果您可以避免用户代理嗅探(提示:CSS 媒体查询),那就这样做。
Although the accepted solution is correct for iPhones, it will incorrectly declare both
isiPhone
andisiPad
to be true for users visiting your site on their iPad from the Facebook app.The conventional wisdom is that iOS devices have a user agent for Safari and a user agent for the UIWebView. This assumption is incorrect as iOS apps can and do customize their user agent. The main offender here is Facebook.
Compare these user agent strings from iOS devices:
Note that on the iPad, the Facebook UIWebView's user agent string includes 'iPhone'.
The old way to identify iPhone / iPad in JavaScript:
If you were to go with this approach for detecting iPhone and iPad, you would end up with IS_IPHONE and IS_IPAD both being true if a user comes from Facebook on an iPad. That could create some odd behavior!
The correct way to identify iPhone / iPad in JavaScript:
We declare IS_IPHONE to be false on iPads to cover for the bizarre Facebook UIWebView iPad user agent. This is one example of how user agent sniffing is unreliable. The more iOS apps that customize their user agent, the more issues user agent sniffing will have. If you can avoid user agent sniffing (hint: CSS Media Queries), DO IT.
我用这个:
I use this:
我用这个:
I use this: