我需要一些函数返回一个布尔值来检查浏览器是否是Chrome。
我如何创建这样的功能?
我需要一些函数返回一个布尔值来检查浏览器是否是Chrome。
我如何创建这样的功能?
当前回答
Var is_chrome = /chrome/。~ . test(navigator.userAgent.toLowerCase()) 这是一个12年前写的老答案;从现在起(即:, 2022年8月)
更新(2022年8月):
现在,使用用户代理(用户代理嗅探)进行浏览器检测通常不是一个好主意。特征检测应该是正确的方法。
参考:使用用户代理进行浏览器检测
然而,如果你仍然希望这样做,使用第三方库,如
var parser = new UAParser(); var result = parser.getResult(); console.log(result.browser);// {name: "Chrome", version: "104.0.0.0", "major": "104"} console.log(result.device); // {model: undefined, type: undefined, vendor: undefined} console.log(result.os); // {name: "Windows", version: "10"} console.log(result.engine); // {name: "Blink", version: "104.0.0.0"} console.log(result.cpu); // {"architecture": "amd64"} <script src="https://cdnjs.cloudflare.com/ajax/libs/UAParser.js/1.0.2/ua-parser.min.js"> </script>
上面的代码段使用了ua-parser-js库
CDN - https://cdnjs.com/lies/uaparser.js - npm - https://www.npmjs.com/package/ua-parser-js
附注:请注意UA嗅探是不好的做法,尽可能使用特征检测。
其他回答
用户可以更改用户代理。尝试在body元素的样式对象中测试webkit的前缀属性
if ("webkitAppearance" in document.body.style) {
// do stuff
}
查询浏览器是否为谷歌Chrome。
var isChrome = navigator.userAgent.includes("Chrome") && navigator.vendor.includes("Google Inc");
console.log(navigator.vendor);
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36 "
console.log(navigator.userAgent);
// "Google Inc."
要检查浏览器是否是谷歌Chrome,试试这个:
// please note,
// that IE11 now returns undefined again for window.chrome
// and new Opera 30 outputs true for window.chrome
// but needs to check if window.opr is not undefined
// and new IE Edge outputs to true now for window.chrome
// and if not iOS Chrome check
// so use the below updated condition
var isChromium = window.chrome;
var winNav = window.navigator;
var vendorName = winNav.vendor;
var isOpera = typeof window.opr !== "undefined";
var isIEedge = winNav.userAgent.indexOf("Edg") > -1;
var isIOSChrome = winNav.userAgent.match("CriOS");
if (isIOSChrome) {
// is Google Chrome on IOS
} else if(
isChromium !== null &&
typeof isChromium !== "undefined" &&
vendorName === "Google Inc." &&
isOpera === false &&
isIEedge === false
) {
// is Google Chrome
} else {
// not Google Chrome
}
使用示例:https://codepen.io/jonathan/pen/RwQXZxJ?editors=1111
这个工作的原因是因为如果你使用谷歌Chrome检查器,并进入控制台选项卡。输入'window'并按enter键。然后你就可以查看“窗口对象”的DOM属性了。当你折叠对象时,你可以看到所有的属性,包括'chrome'属性。
你不能再用strict equals true来检入IE中的window.chrome。IE以前返回undefined,现在返回true。但是你猜怎么着,IE11现在又返回undefined了。IE11还为window.navigator.vendor返回一个空字符串""。
更新:
Thank you to Halcyon991 for pointing out below, that the new Opera 18+ also outputs to true for window.chrome. Looks like Opera 18 is based on Chromium 31. So I added a check to make sure the window.navigator.vendor is: "Google Inc" and not is "Opera Software ASA". Also thanks to Ring and Adrien Be for the heads up about Chrome 33 not returning true anymore... window.chrome now checks if not null. But play close attention to IE11, I added the check back for undefined since IE11 now outputs undefined, like it did when first released.. then after some update builds it outputted to true .. now recent update build is outputting undefined again. Microsoft can't make up it's mind!
更新7/24/2015 -增加Opera检查
Opera 30刚刚发布。它不再输出window.opera。还有窗户。chrome在新的Opera 30中输出为true。因此您必须检查OPR是否在userAgent中。我更新了上面的条件,以解释Opera 30中的这个新变化,因为它使用与谷歌Chrome相同的渲染引擎。
更新10/13/2015 -添加IE检查
添加检查IE边缘,因为它输出真窗口。chrome . .即使IE11的window.chrome输出未定义。感谢artfulhacker让我们知道这一点!
更新2/5/2016 -添加iOS Chrome检查
添加检查iOS Chrome检查CriOS,因为它在iOS上的Chrome输出为真。感谢xinthose让我们知道这件事!
更新4/18/2018 -更改Opera检查
编辑检查Opera,检查窗口。opr没有定义,因为现在Chrome 66有opr在window.navigator.vendor。感谢Frosty Z和Daniel Wallman的报道!
如果你有勇气,你可以尝试浏览器嗅探,得到一个版本:
var ua = navigator.userAgent;
if(/chrome/i.test(ua)) {
var uaArray = ua.split(' ')
, version = uaArray[uaArray.length - 2].substr(7);
}
这个检测到的版本可能是Chrome版本,或者Edge版本,或者其他版本。浏览器插件可以很容易地改变userAgent、平台和其他东西,所以不建议这样做。
向绿脚趾向我道歉,我用了他的答案。
var is_chrome = browseris.chrome
或者查看浏览器:
browseris.firefox
browseris.ie
browseris.safari
此外,你可以像browseris一样检查版本。Chrome7up等等。
检查browseris对象中的所有现有信息