我需要一些函数返回一个布尔值来检查浏览器是否是Chrome。
我如何创建这样的功能?
我需要一些函数返回一个布尔值来检查浏览器是否是Chrome。
我如何创建这样的功能?
当前回答
var is_chrome = browseris.chrome
或者查看浏览器:
browseris.firefox
browseris.ie
browseris.safari
此外,你可以像browseris一样检查版本。Chrome7up等等。
检查browseris对象中的所有现有信息
其他回答
要检查浏览器是否是谷歌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 is_chrome = /chrome/i。测试(导航器。userAgent);
从Chrome 89(2021年3月)开始,所有以前的答案都已过时。Chrome现在支持用户代理提示。所以现在应该使用:
navigator.userAgentData?.brands?.some(b => b.brand === 'Google Chrome')
或者,如果你不使用Babel:
navigator.userAgentData && navigator.userAgentData.brands && navigator.userAgentData.brands.some(b => b.brand === 'Google Chrome')
对于Chrome 89及以上版本返回true,对于最新的Opera和Edge返回false,对于不支持userAgentData的浏览器返回undefined。
在Mac上的Chrome上为我工作。似乎比上面所有的都更简单或更可靠(如果userAgent字符串测试)。
var isChrome = false;
if (window.chrome && !window.opr){
isChrome = true;
}
console.log(isChrome);
现在你可以使用navigator.userAgent.includes(“Chrome”)