Javascript Javascript检测android本机浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14701951/
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
Javascript detect android native browser
提问by Razvan
I want to detect with javascript the native android browser (the one that comes installed on every android phone).
我想用 javascript 检测原生 android 浏览器(安装在每个 android 手机上的浏览器)。
What should I look for in useragent ?
我应该在 useragent 中寻找什么?
回答by PRASS
This should work.
这应该有效。
var nua = navigator.userAgent;
var is_android = ((nua.indexOf('Mozilla/5.0') > -1 && nua.indexOf('Android ') > -1 && nua.indexOf('AppleWebKit') > -1) && !(nua.indexOf('Chrome') > -1));
回答by Kailas
Native browser, we could not detect at the first go itself, using the above mentioned code:
本机浏览器,我们无法在第一次检测到本身,使用上面提到的代码:
var nua = navigator.userAgent;
var is_android = ((nua.indexOf('Mozilla/5.0') > -1 && nua.indexOf('Android ') > -1 && nua.indexOf('AppleWebKit') > -1) && !(nua.indexOf('Chrome') > -1));
as for most devices we got the following as the user agent for chrome browser:
对于大多数设备,我们将以下内容作为 chrome 浏览器的用户代理:
and in the native browser:

并在本机浏览器中:

And carried out this observation for Samsung Galaxy S3, htc desire, Asus Zenfone5.
并针对三星 Galaxy S3、HTC 欲望、华硕 Zenfone5 进行了此项观察。
And found out that the "Chrome/30.0.0.0" or the "chrome/" along withthe version is present for most devces including Zenfone 5, Samsung Galaxy s3. But the "version/" no. present in the user agent object is more than enough to differentiate the native and the Chrome.
并发现“Chrome/30.0.0.0”或“chrome/”以及该版本适用于包括 Zenfone 5、Samsung Galaxy s3 在内的大多数设备。但是“版本/”没有。存在于用户代理对象中足以区分原生和 Chrome。
We used the following code:
我们使用了以下代码:
var ua = navigator.userAgent;
var is_native_android = ((ua.indexOf('Mozilla/5.0') > -1 && ua.indexOf('Android ') > -1 && ua.indexOf('AppleWebKit') > -1) && (ua.indexOf('Version') > -1));
Hope, it's useful for you too... :)
希望,它对你也有用... :)
回答by Jamir Khan
Try something like this:
尝试这样的事情:
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
// Do something
}
or
或者
check this url: http://davidwalsh.name/detect-android
检查这个网址:http: //davidwalsh.name/detect-android

