我想要检测浏览器是否是chrome。我遇到了很多答案,但它们都返回true,对于opera和新版edge也是如此。我想知道你是否有最好的方法来检测浏览器是否是chrome
编辑到目前为止我测试过的返回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 );
发布于 2020-06-17 10:42:33
我建议您参考此代码示例,它可以帮助您识别Ms Edge旧版、MS Edge Chromium、Opera、Google Chrome、IE、Firefox和Safari浏览器。
<!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>
MS Edge (Chromium)浏览器中的输出:

Google Chrome浏览器中的输出:

Opera浏览器中的输出:

参考资料:
What is the correct way to detect new Microsoft Edge v80 (Blink) via Javascript?
https://stackoverflow.com/questions/62409889
复制相似问题