Reputation: 29557
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.
Upvotes: 7
Views: 9384
Reputation: 1103
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
Upvotes: 0
Reputation: 5413
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.
Upvotes: 0
Reputation: 1539
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 ?
Upvotes: 0
Reputation: 1216
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.
Upvotes: 3
Reputation: 29557
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!
Upvotes: 9
Reputation: 478
(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.)
Upvotes: 0
Reputation: 2323
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.
Upvotes: 0