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.
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.
Share Improve this question edited Jul 24, 2014 at 10:46 user984003 asked Jul 24, 2014 at 6:05 user984003user984003 29.6k69 gold badges202 silver badges313 bronze badges 6- possible duplicates stackoverflow.com/questions/9286355/… stackoverflow.com/questions/14403766/… – trainoasis Commented Jul 24, 2014 at 6: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 Commented Jul 24, 2014 at 6: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 Commented Jul 24, 2014 at 6: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 Commented Jul 24, 2014 at 6:40
- @jfriend00 IRL, many of those JS "features" fail silently on mobile browsers. ibm.com/developerworks/library/wa-ioshtml5 – yPhil Commented Sep 26, 2015 at 12:11
7 Answers
Reset to default 9So 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!
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.
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.
(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.)
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:
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 ?
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.
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