22

it's easy to detect the Android device, but I am having trouble detecting ONLY the Android native browser. 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..

Is this possible?

williamsandonz
  • 13,581
  • 21
  • 88
  • 171
  • possible duplicate of [Auto detect mobile browser (via user-agent?)](http://stackoverflow.com/questions/1005153/auto-detect-mobile-browser-via-user-agent) – Phill Pafford Feb 15 '12 at 01:45
  • 5
    I don't think so? that's a general strategy. This for JUST the native browser on Android 2.X, 3.X, 4.X – williamsandonz Feb 15 '12 at 02:56
  • http://www.zytrax.com/tech/web/mobile_ids.html You should be able to catch the user agent, maybe the distro as well. But as with Android this looks to be easily configurable – Phill Pafford Feb 15 '12 at 03:32
  • thanks for the link, its an interesting read but I don't think it helps me. There's stuff in there about Dolphin but it's not going to be accurate enough to help. It's a shame, I think there is no user-agent methodology to identify between Dolphin on Android 2.x and Android 2.x native. The only way I can now think of, is that Dolphin's header is effectively twice as tall as the natives so could figure that out. – williamsandonz Feb 16 '12 at 20:35
  • you could use target-dpi media queries that only android supports, but they have been deprecated so that may not be optimal for future versions – albert Mar 11 '13 at 07:17
  • Why are you trying to see if they are using that particular browser? Is it something you can feature test? – Jeremy J Starcher Mar 20 '13 at 09:01
  • See also http://stackoverflow.com/questions/14403766/how-to-detect-the-stock-android-browser – That Brazilian Guy Apr 08 '14 at 10:41
  • Btw Dolphin browser is a webview of the stock/native browser – Bnaya Jun 26 '14 at 22:20

5 Answers5

19

you simply need to test a few parts of the user agent string in order to make sure you have the default android browser:

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.

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));
bizzehdee
  • 17,878
  • 9
  • 41
  • 70
  • 5
    This will return true even for Chrome browser in Android? – HashCoder Apr 04 '13 at 04:24
  • chrome is the default browser once you get to 4.1 – bizzehdee Apr 04 '13 at 10:33
  • 1
    That doesn't seem to be the case. I have a Samsung Galaxy S3 running android 4.1.2 and the default browser is the android browser (not chrome). – Ali Gangji Jun 04 '13 at 13:55
  • 1
    sometimes useragent returns AppleWebkit (lowercase on kit) and this fails: ua.indexOf('AppleWebKit') > -1. Probably best to do a regex instead of indexOf. – ds111 Feb 04 '14 at 23:17
  • 1
    This will work with regex `var nua = navigator.userAgent;` `var is_android = ((nua.match(/Mozilla\/5\.0/gi) !== null && nua.match(/Android/gi) !== null && nua.match(/AppleWebKit/gi) !== null) && (nua.match(/Chrome/gi) !== null));` PS How to make new line? Two spaces doesn't work :/ – Arek - Krakiewicz.pl Jul 07 '15 at 08:38
  • since we are looking for particular, unchanging strings, using match is going to be quite a but slower than indexOf. Also, you cant do new lines in comments. – bizzehdee Jul 07 '15 at 13:19
  • 1
    this did not detect the default android browser for Samsung Galaxy S5 (4.4.2) – Dayne Mentier Jul 09 '15 at 21:22
2

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)

Here's my sample JavaScript code:

(If you need specific styling I would add a class to the body with the following JS snippet)

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)

Peter MK
  • 134
  • 5
2

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. 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))
carterh062
  • 180
  • 10
  • 1
    YES - this is the only actually correct answer. All of the others assume Chrome is not mentioned in the UA - which is not true. – itamar Mar 14 '16 at 08:33
  • Unfortunately it did'nt work on Samsung S5 Mini Android 4.4.2 because there was "Chrome" in the user agent string when using the Android Default Browser... – user2718671 Apr 13 '16 at 13:12
  • ...and on Samsung Galaxy Note S3 there is no "Version" string – user2718671 Apr 13 '16 at 13:22
-7
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';
}
Brian Lewis
  • 152
  • 2
  • 4
  • 1
    This just detects Android in general, not differences between different browsers available on Android (which was what the original question was looking for). – Scott Aug 26 '13 at 15:56
-11

You can do this with Javascript and the useragent feature. What u need to do is to make 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 chrome, firefox, safari and so on
   Take this setting

Thats it in the theory :)

Matthew R.
  • 4,152
  • 1
  • 20
  • 39