Reputation: 171
I want to detect if the browser is chrome. I came across a lot of answers but all of them return true also for opera and the new version of edge. I was wondering if you have the best way to detect if the browser is chrome
[edit] Code i've tested so far that return true :
//Test 1 => Opera and edge returns true
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
// Test 2 => return false for chrome latest version
var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
//Test 3 => same as test 1
var is_chrome = /chrome/i.test( navigator.userAgent );
Upvotes: 0
Views: 5329
Reputation: 11355
I suggest you can refer to this code example that may help you to identify the MS Edge legacy, Ms. Edge Chromium, Opera, Google Chrome, IE, Firefox, and Safari browser.
<!doctype html>
<html>
<head>
<title>Test demo</title>
</head>
<body>
<script>
var browser = (function (agent) {
switch (true) {
case agent.indexOf("edge") > -1: return "MS Edge (EdgeHtml)";
case agent.indexOf("edg") > -1: return "MS Edge Chromium";
case agent.indexOf("opr") > -1 && !!window.opr: return "opera";
case agent.indexOf("chrome") > -1 && !!window.chrome: return "chrome";
case agent.indexOf("trident") > -1: return "Internet Explorer";
case agent.indexOf("firefox") > -1: return "firefox";
case agent.indexOf("safari") > -1: return "safari";
default: return "other";
}
})(window.navigator.userAgent.toLowerCase());
document.body.innerHTML = "This is " + browser + " browser." + "<br><br>" + window.navigator.userAgent.toLowerCase();
</script>
</body>
</html>
Output in MS Edge (Chromium) browser:
Output in Google Chrome browser:
Output in Opera browser:
Reference:
What is the correct way to detect new Microsoft Edge v80 (Blink) via Javascript?
Upvotes: 4