简体   繁体   English

如何只检测原生Android浏览器

[英]How to detect only the native Android browser

it's easy to detect the Android device, but I am having trouble detecting ONLY the Android native browser. 它很容易检测到Android设备,但我只能检测到Android原生浏览器。 Problem is the Dolphin browser has an almost identical user-agent string, and I'd like a way to know if they are using the native browser or not.. 问题是Dolphin浏览器有一个几乎相同的用户代理字符串,我想知道他们是否使用本机浏览器。

Is this possible? 这可能吗?

you simply need to test a few parts of the user agent string in order to make sure you have the default android browser: 你只需要测试用户代理字符串的一些部分,以确保你有默认的android浏览器:

var nua = navigator.userAgent;
var is_android = (nua.indexOf('Mozilla/5.0') > -1 && nua.indexOf('Android ') > -1 && nua.indexOf('AppleWebKit') > -1);

you can use the following to ensure that you do not match chrome within android, although on a lot of devices now, chrome is being used as the default browser. 你可以使用以下内容来确保你在android中不匹配chrome,虽然现在在很多设备上,chrome被用作默认浏览器。

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));

EDIT: If you want to protect against case sensitivity, you can use the following: 编辑:如果要防止区分大小写,可以使用以下命令:

var nua = navigator.userAgent.toLowerCase();
var is_android = ((nua.indexOf('mozilla/5.0') > -1 && nua.indexOf('android ') > -1 && nua.indexOf('applewebkit') > -1) && !(nua.indexOf('chrome') > -1));

I think you are searching for this: 我想你正在寻找这个:

Android native browser not updated above version 534.30 so you can filter to the version and Android UA string combination (above we can presume its a Chrome browser) Android原生浏览器未在版本534.30上更新,因此您可以过滤到版本和Android UA字符串组合(上面我们可以假设它是Chrome浏览器)

Here's my sample JavaScript code: 这是我的示例JavaScript代码:

(If you need specific styling I would add a class to the body with the following JS snippet) (如果你需要特定的样式,我会使用以下JS片段向主体添加一个类)

var defectAndroid = $window.navigator && $window.navigator.userAgent.indexOf('534.30') > 0 && $window.navigator.userAgent.toLowerCase().match(/android/);

if (defectAndroid) {
   // sample code specific for your Android Stock browser
}

(Some Android devices reporting 'android' that's why we need the lower case conversation) (一些Android设备报告'android',这就是我们需要小写对话的原因)

On a Galaxy S3, I found that both Chrome and the native browser had 'AppleWebkit' so I took that piece out of my conditional statement. 在Galaxy S3上,我发现Chrome和本机浏览器都有'AppleWebkit',因此我从条件语句中删除了该文件。 I also added Version as that only appears in the native browser is seems. 我还添加了版本,因为它只出现在本机浏览器中。 It works for me as 它对我有用

var ua = navigator.userAgent;
var isAndroidNative = ((ua.indexOf('Mozilla/5.0') > -1) && (ua.indexOf('Android') > -1) && !(ua.indexOf('Chrome') > -1) && (ua.indexOf('Version') > -1))
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
    // Do something!
    // Redirect to Android-site?
    window.location = 'http://android.davidwalsh.name';
}

You can do this with Javascript and the useragent feature. 您可以使用Javascript和useragent功能执行此操作。 What u need to do is to make 2 If-requests: 你需要做的是提出2个If-requests:

First you detect the device type: 首先检测设备类型:

If android, ios, mobile, ipad, iphone 
   Take this setting

Now you make as much as u need if-requests or a case-request to detect the type of browser 现在,您可以根据需要提出if-requests或case-request来检测浏览器的类型

If chrome, firefox, safari and so on
   Take this setting

Thats it in the theory :) 多数民众赞成在理论上:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM