在移动 Safari 中检测 iOS5(首选 JavaScript)

发布于 2024-12-10 19:01:10 字数 99 浏览 0 评论 0原文

iOS5 中引入的新固定定位损坏了我的网络应用程序,我需要一种方法来检测 iOS5 用户。

如何检测iOS5?浏览器代理字符串是什么?首选 JavaScript。谢谢!

The new fixed positioning introduced in iOS5 corrupted my webapp and I need a way to detect iOS5 users.

How can I detect iOS5? What is the browser agent string? JavaScript preferred. Thanks!

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

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

发布评论

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

评论(4

初懵 2024-12-17 19:01:10

来自 SO 问题:iOS 5 用户代理字符串是什么

iPhone:

Mozilla/5.0(iPhone;CPU iPhone OS 5_0,如 Mac OS X)AppleWebKit/534.46(KHTML,如 Gecko)版本/5.1 Mobile/9A334 Safari/7534.48.3

iPad:

Mozilla/5.0(iPad;CPU OS 5_0,如 Mac OS X)AppleWebKit/534.46(KHTML,如 Gecko)版本/5.1 Mobile/9A334 Safari/7534.48.3

以下是在 javascript 中测试 ios 5.x 的方法:

<script type="text/javascript">
    if (navigator.userAgent.match(/OS 5(_\d)+ like Mac OS X/i))
        // this helps detect minor versions such as 5_0_1
        document.write("You have iOS 5! Aren't you special!");
</script>

From the SO question: What is the iOS 5 user-agent string:

iPhone:

Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3

iPad:

Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3

And here is way to test for ios 5.x in javascript:

<script type="text/javascript">
    if (navigator.userAgent.match(/OS 5(_\d)+ like Mac OS X/i))
        // this helps detect minor versions such as 5_0_1
        document.write("You have iOS 5! Aren't you special!");
</script>
狠疯拽 2024-12-17 19:01:10

我进一步开发了 chown 的答案,检查任何 iOS5 设备,无论是 5 或 5.0.1 或任何更高版本:

var ios5 = navigator.userAgent.match(/OS 5_[0-9_]+ like Mac OS X/i) != null;
if(ios5) {
    // Now you can do specific stuff for devices on any version of iOS5
}

I've developed chown's answer a bit further, with a check for any iOS5 device, be it 5 or 5.0.1 or any later versions:

var ios5 = navigator.userAgent.match(/OS 5_[0-9_]+ like Mac OS X/i) != null;
if(ios5) {
    // Now you can do specific stuff for devices on any version of iOS5
}
孤檠 2024-12-17 19:01:10

您可以使用 Modernizr 并检测 Web Workers,因为它们在 iOS 5 之前不可用。您仍需要排除 Android,因为 Android 2.0+ 已支持它们。这将为您提供与 iOS6 的前向兼容性,而使用任何已提供的用户代理解析都无法实现这一点。

You could be using Modernizr and detecting for Web Workers as they were not available prior to iOS 5. You need to still exclude Android as Android 2.0+ has supported them. This will give you forward compatibility with iOS6 which you aren't going to get with either of the user agent parses already provided.

愚人国度 2024-12-17 19:01:10

您需要考虑“CPU OS 5_0”和“CPU iPhone OS 5_0”,粗糙且肮脏的正则表达式:

<script type="text/javascript">
if (navigator.userAgent.match(/(iPad|iPhone);.*CPU.*OS 5_\d/i))
{ 
   alert("On iOS 5")
}
else
{
   alert("Not on iOS 5"); //could be either 4- or 6+
}
</script>

You need to account for "CPU OS 5_0" and "CPU iPhone OS 5_0", rough and dirty regex:

<script type="text/javascript">
if (navigator.userAgent.match(/(iPad|iPhone);.*CPU.*OS 5_\d/i))
{ 
   alert("On iOS 5")
}
else
{
   alert("Not on iOS 5"); //could be either 4- or 6+
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文