Javascript 检测浏览器是否在 Android 或 iOS 设备上运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12606245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:48:32  来源:igfitidea点击:

Detect if browser is running on an Android or iOS device

javascriptjqueryhtmlbrowser

提问by soleil

I need to change out some buttons and text on a mobile website depending on whether the user is viewing it on an Android or iOS browser. Is there a reliable way to perform the check?

我需要更改移动网站上的一些按钮和文本,具体取决于用户是在 Android 还是 iOS 浏览器上查看。有没有可靠的方法来执行检查?

回答by Anoop

var isMobile = {
    Windows: function() {
        return /IEMobile/i.test(navigator.userAgent);
    },
    Android: function() {
        return /Android/i.test(navigator.userAgent);
    },
    BlackBerry: function() {
        return /BlackBerry/i.test(navigator.userAgent);
    },
    iOS: function() {
        return /iPhone|iPad|iPod/i.test(navigator.userAgent);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
    }
};

回答by Christofer Eliasson

I would use the navigator.useragentproperty to get the useragent-string, parse it, and detect the OS.

我将使用该navigator.useragent属性来获取用户代理字符串、解析它并检测操作系统。

Hereis one example of how you can do it.

是您如何做到一点的一个示例。

Update:

更新:

Due to the popularity of this question, I thought I would add some additional information to my answer.

由于这个问题的流行,我想我会在我的答案中添加一些额外的信息。

In general, browser sniffing is almost never the best way to go. Why? Well, there are many reasons, but here are two good ones:

一般来说,浏览器嗅探几乎从来都不是最好的方法。为什么?嗯,有很多原因,但这里有两个很好的原因:

  1. In almost all cases when people turn to browser sniffing, feature detection would be a better option. There are rare edge cases where it might be appropriate to act depending on what OS/browser the user might be using, but for the most part, using a library like Modernizrto detect support for a specific feature is a better way to go. Trying to adapt your site/application to specific browsers rather than the features they support is a slippery slope. Instead check if the browser support the features you need, and polyfill or provide nice fallbacks when support is lacking.
  2. Browser sniffing is extremely complicated. Why? Because almost every browser lie in their user agent string or lack sufficient information to allow a certain identification.
  1. 在人们转向浏览器嗅探的几乎所有情况下,特征检测将是更好的选择。在极少数情况下,根据用户可能使用的操作系统/浏览器采取行动可能是合适的,但在大多数情况下,使用像Modernizr这样的库来检测对特定功能的支持是更好的方法。尝试使您的站点/应用程序适应特定浏览器而不是它们支持的功能是一种滑坡。而是检查浏览器是否支持您需要的功能,并在缺乏支持时填充或提供不错的回退。
  2. 浏览器嗅探极其复杂。为什么?因为几乎每个浏览器都存在于它们的用户代理字符串中,或​​者缺乏足够的信息来允许进行某种识别。

With that said, if you really really need to use browser/OS detection, then don't reinvent the wheel, and don't try to do it on your own - you will be in for a world of pain and obscure caveats! I would suggest you use a library like WhichBrowser, that will provide you with a handy JavaScript object containing information about the os, browser, rendering engine and device.

话虽如此,如果您真的真的需要使用浏览器/操作系统检测,那么不要重新发明轮子,也不要尝试自己做 - 您将进入一个充满痛苦和模糊警告的世界!我建议您使用像whichBrowser这样的库,它将为您提供一个方便的 JavaScript 对象,其中包含有关操作系统、浏览器、渲染引擎和设备的信息。

回答by Christofer Eliasson

You could use the navigator object: http://www.w3schools.com/js/js_browser.aspand then use if statements based on the properties of navigator.

您可以使用导航器对象:http: //www.w3schools.com/js/js_browser.asp,然后根据导航器的属性使用 if 语句。

回答by meme

I have used this for awhile. Simple check to see return true or false based on is a mobile device.

我已经使用了一段时间。基于移动设备的简单检查以查看返回 true 或 false。

var isMobile = (/Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Windows Phone/i.test(navigator.userAgent)) ? true : false;