6

I need to tell what browser the users are using for my website. (edit: Users need to add a bookmarklet, which is not possible on the standard 'internet' browser. I need to know what message to show them.)

EDIT: I don't need to be able to detect any kind of browser. Specifically I need, in this case, to be able to detect whether a browser is truly a Google Chrome browser.

For at least one smart device, I am having trouble telling the difference between the stock 'internet' browser and Chrome; Both contain the word 'Chrome'.

Samsung galaxy s5:

Stock browser user agent:

Mozilla/5.0 (Linux; Android 4.4.2; en-us; SAMSUNG-SM-G900A Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.6 Chrome/28.0.1500.94 Mobile Safari/537.36

Chrome user agent:

Mozilla/5.0 (Linux; Android 4.4.2; SAMSUNG-SM-G900A Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.128 Mobile Safari/537.36

"Version/X.x" is different, but will that always be the case?

Edit: I did already check for previous answers as suggested in the comments. They assume that the non-Chrome browser does NOT contain the word Chrome.

user984003
  • 28,050
  • 64
  • 189
  • 285
  • possible duplicates http://stackoverflow.com/questions/9286355/how-to-detect-only-the-native-android-browser http://stackoverflow.com/questions/14403766/how-to-detect-the-stock-android-browser – trainoasis Jul 24 '14 at 06:13
  • 1
    It is generally a bad idea to try to detect a specific type of browser. It is much better to detect the things that actually matter to your code such as the presence or lack of certain features (called feature detection) and base your logic on what features are present rather than what browser you think it is. – jfriend00 Jul 24 '14 at 06:24
  • 1
    So what would be a feature that is different between these two browsers? I need to know whether users can install a bookmarklet, but that is not something that I can check for. – user984003 Jul 24 '14 at 06:30
  • 1
    @trainoasis I do check for previous answers before I ask my own. The ones you mention do not answer the question. (Sometimes people are VERY quick to mark a question as duplicate without actually checking. This hurts the chances of the new question being answered.) – user984003 Jul 24 '14 at 06:40
  • @jfriend00 IRL, many of those JS "features" fail **silently** on mobile browsers. http://www.ibm.com/developerworks/library/wa-ioshtml5/ – yPhil Sep 26 '15 at 12:11
  • @xaccrocheur - proper feature detection works just as well on mobile browsers as it does on desktop browsers - that's the whole point. You test to see if the desired feature is present and working rather than testing which browser it is. – jfriend00 Sep 26 '15 at 14:53

7 Answers7

8

So the difference is this in the user agent:

Version/X.X

From https://developer.chrome.com/multidevice/user-agent#webview_user_agent:

"If you’re attempting to differentiate between the WebView and Chrome for Android, you should look for the presence of the Version/X.X string in the WebView user-agent string."

I suppose a Chrome webview browser can still choose to leave that bit out, but if it DOES have it, then at least I know it isn't true Google Chrome!

user984003
  • 28,050
  • 64
  • 189
  • 285
  • I tested it on few phones and it is working! Thanks! – Technotronic Dec 31 '14 at 15:06
  • 1
    I'm not convined the `Version/_X.X_` heuristic is accurate. I'm testing an HTC One SV with the latest stock browser and it doesn't have `Version/_X.X_` in the UA: `mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.34 Safari/534.24` – Sam Hasler May 01 '15 at 12:13
  • 1
    I am not sure you understand what a WebView is but it surely is not the Android Stock browser. There is no way to detect it safely. At best with multiple checks you will get a 60-80% hit. – Arnaud Leyder Sep 11 '15 at 07:32
3

Please check this link: https://developer.chrome.com/multidevice/user-agent In the last paragraph we find:

If you’re attempting to differentiate between the WebView and Chrome for Android, you should look for the presence of the Version/X.X string in the WebView user-agent string.

Alexandre M
  • 1,146
  • 12
  • 23
0

Since I'm not aware of any blatant feature differences between the stock browser and chrome, playing 'spot the difference' with the user agent string reveals that the Chrome version stated in the stock browser is quite a few versions back.

Whilst its not the best difference and the stock browser could always update, at least at the moment, this may work ok for you.

... you haven't really said how you will be using this.

Dylan Watson
  • 2,283
  • 2
  • 20
  • 38
  • Users need to add a bookmarklet, which isn't possible on the standard 'internet browser'. So I need to know what message to write. @Dylan Watson: that slide is funny (and sad). – user984003 Jul 24 '14 at 10:13
  • 1
    You haven't tried to play audio in a webpage, then :) – yPhil Sep 26 '15 at 12:09
0

(I'm not allowed to comment, so I write an answer instead.)

User984003 so, if I understand you right (from your comment), you want essentially a test to distinguish, on mobile devices, between stock browsers and an browser installed by the user. You want this because there is a difference between them in the availableness of respectively the permission to install bookmarklets, right?

Then this topic is discussed in this thread: How to detect the stock Android browser (The short and unsatisfactory answer in this thread was: “There is no reliable and universal way to detect only stock browsers.”)

Additionally here some informations on ways to install bookmarklets on android: http://smallbusiness.chron.com/making-bookmarklets-work-android-50685.html (Maybe here are some alternatives you are not aware of, which makes the attempts to distinguish the browser versions obsolete.)

Community
  • 1
  • 1
theafh
  • 478
  • 2
  • 7
  • While you say this is not an answer, I believe it is one, as it does answer the question: "(The short and unsatisfactory answer in this thread was: “There is no reliable and universal way to detect only stock browsers.”)" – Léo Lam Jul 24 '14 at 10:21
  • What I need is not so much to be able to detect ANY kind of browser: What I need is to be able to detect whether something is the 'real' Chrome browser when I see the word Chrome in the User agent. – user984003 Jul 24 '14 at 10:44
0

Read a lot of stuff, and it seems that they all don't do much and it's still a problem to see the difference. I searched the net, no solution.

I went to check this on my own, and found this:

Differences between native and Chrome

So I found out that native browser (on Samsung S4) did append SamsungBrowser after "Gecko)".

On Chrome, its "like Gecko) Chrome".

So immediately after "Gecko)", it's " Chrome".

Maybe that could do the trick since Samsung appends it's own string into the user agent.

Is it possible that other devices could again cause problems ?

Adrian
  • 1,499
  • 3
  • 19
  • 26
0

You can use JavaScript e.g.

var chromeVersion = /^Google/.test(navigator.vendor) && !/ OPR/.test(navigator.userAgent) && /Chrome\/([0-9]+|$)/.exec(navigator.userAgent)[1];
var isChromeWebview = chromeVersion >= 30 && !('chrome' in window);

window.chrome is defined in the Chrome Browser (from at least Chrome 10) but it is not defined in Chrome WebView (Chrome was first used for the WebView on Android 4.4.x; AOSP was used for the WebView for Android 4.3 or less).

I personally wouldn't use that test in production (it will be unreliable for different browsers and versions), but it does what you want if you use it for a purpose that isn't critical.

robocat
  • 5,293
  • 48
  • 65
0

I'd recommend something like this:

function testForAndroidChrome() {
    var version = navigator.appVersion;
    if (version.indexOf('(KHTML, like Gecko) Chrome' != -1) {
        return true; // it's Chrome
    } else {
        return false; // it's the stock browser
    }
}

var myTest = testForAndroidChrome(); // returns true or false
tonejac
  • 1,083
  • 3
  • 18
  • 32