JavaScript:如何确定用户浏览器是否为Chrome?

Rel*_*lla 208 html javascript google-chrome browser-detection

我需要一些函数返回一个布尔值来检查浏览器是否是Chrome.

我如何创建这样的功能?

Jon*_*llo 296

要检查浏览器是否为Google 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("Edge") > -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 
}
Run Code Online (Sandbox Code Playgroud)

使用示例:http://codepen.io/jonathan/pen/WpQELR

这样做的原因是因为如果您使用Google Chrome检查器并转到控制台选项卡.输入"窗口",然后按Enter键.然后,您就可以查看"窗口对象"的DOM属性.折叠对象时,您可以查看所有属性,包括"chrome"属性.

您不能再使用严格等于true来检查IE window.chrome.IE用来返回undefined,现在它返回true.但是猜猜看,IE11现在再次返回undefined.IE11还返回一个空字符串""window.navigator.vendor.

我希望这有帮助!

更新:

感谢Halcyon991在下面指出,新的Opera 18+也输出为true window.chrome.看起来Opera 18基于Chromium 31.所以我添加了一张支票以确保它window.navigator.vendor是:"Google Inc"而不是"Opera Software ASA".还要感谢RingAdrien Be对于Chrome 33不再回归的问题...... window.chrome现在检查是否为空.但是密切注意IE11,我添加了检查,undefined因为IE11现在输出undefined,就像它在第一次发布时所做的那样..然后在一些更新构建后输出到true..现在最近的更新版本undefined再次输出.微软无法弥补它的想法!

更新 2015年7月24日 - Opera检查的补充

Opera 30刚刚发布.它不再输出window.opera.并且window.chrome在新的Opera 30中输出为true.因此,您必须检查OPR是否在userAgent中.我更新了上面的条件,以解释Opera 30中的这一新变化,因为它使用与Google Chrome相同的渲染引擎.

更新 2015年10月13日 - IE检查的补充

增加了检查IE边缘,由于其输出truewindow.chrome..即使IE11输出undefinedwindow.chrome.感谢artfulhacker让我们知道这件事!

更新 2016年2月5日 - iOS Chrome检查添加

添加了对iOS Chrome检查的检查,CriOS因为它true在iOS上输出Chrome.感谢xinthose让我们知道这件事!

更新 4/18/2018 - 更改Opera检查

编辑检查Opera,检查window.opr是不是undefined从现在Chrome 66已经OPR进入window.navigator.vendor.感谢Frosty ZDaniel Wallman举报此事!

  • 最新的 Edge 用户代理值实际上是“Edg”而不是“Edge”(另请参阅这些文档:https://docs.microsoft.com/en-us/microsoft-edge/web-platform/user-agent-string) 。因此,也许这一行:`inNav.userAgent.indexOf("Edge")`应该更改为`inNav.userAgent.indexOf("Edg")`。 (6认同)
  • var isGoogleChrome = window.chrome!= null && window.navigator.vendor ==="Google Inc."; (2认同)
  • 也许与Daniel Wallman在这里有同样的问题:我的Android Chrome用户代理包含"OPR"字符串!`Mozilla/5.0(Linux; Android 8.0.0; ASUS_Z012D Build/OPR1.170623.026)AppleWebKit/537.36(KHTML,和Gecko一样)Chrome/65.0.3325.109 Mobile Safari/537.36`,因此`isChrome()`返回*false*. (2认同)
  • 感谢@FrostyZ和@DanielWallman让我们知道.我修复了它,所以Opera检查`window.opr`不是'undefined`. (2认同)

Rio*_*ams 195

更新:请参阅Jonathan的答案,了解更新方法.下面的答案可能仍然有效,但它可能会在其他浏览器中引发一些误报.

var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
Run Code Online (Sandbox Code Playgroud)

但是,如上所述,用户代理可能会被欺骗,因此在处理这些问题时最好使用功能检测(例如Modernizer),正如其他答案所述.

  • @Serg,因为他们没有Chrome.它只是iOS Safari的皮肤. (7认同)
  • 在 Microsoft Edge 中返回 `true`。 (4认同)
  • 由于很多浏览器都返回true,这里是我使用的代码,它排除了Edge,Maxthon,iOS safari等等`var is_chrome =((navigator.userAgent.toLowerCase().indexOf('chrome')> -1 )&&(navigator.vendor.toLowerCase().indexOf("google")> -1));` (4认同)
  • iphone 5,ios 5,镀铬.解决方案不起作用. (2认同)
  • 谢谢,虽然你的var名称应该是camelCase (2认同)
  • Opera(至少版本 42)将 `Google Inc` 返回给 `navigator.vendor`,所以这个方法不是防弹的,类似于 `/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor) ) && !/OPR/.test(navigator.userAgent)` 可能会更好 (2认同)

小智 22

甚至更短: var is_chrome = /chrome/i.test( navigator.userAgent );

  • 在Microsoft Edge中返回`true`. (24认同)

Dre*_*kes 14

一个更简单的解决方案就是使用:

var isChrome = !!window.chrome;
Run Code Online (Sandbox Code Playgroud)

!!刚刚转换的对象为布尔值.在非Chrome浏览器中,此属性将undefined是不真实的.

  • 嗯,Opera基本上是Chrome (6认同)
  • Opera实际上将`true`返回到`window.chrome`.查看具有防弹检测+修复的conditionizr.com. (5认同)
  • 这也会为 Edge 返回“true”。 (3认同)
  • @vishalsharma,“ !!”将值转换为“ true”或“ false”。`typeof(window.chrome)`给出“对象”,而`typeof(!! window.chrome)`给出“布尔值”。您的代码示例也可以工作,因为`if`语句可以进行转换。 (2认同)

Jos*_*žek 14

console.log(JSON.stringify({
  isAndroid: /Android/.test(navigator.userAgent),
  isCordova: !!window.cordova,
  isEdge: /Edge/.test(navigator.userAgent),
  isFirefox: /Firefox/.test(navigator.userAgent),
  isChrome: /Google Inc/.test(navigator.vendor),
  isChromeIOS: /CriOS/.test(navigator.userAgent),
  isChromiumBased: !!window.chrome && !/Edge/.test(navigator.userAgent),
  isIE: /Trident/.test(navigator.userAgent),
  isIOS: /(iPhone|iPad|iPod)/.test(navigator.platform),
  isOpera: /OPR/.test(navigator.userAgent),
  isSafari: /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent),
  isTouchScreen: ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch,
  isWebComponentsSupported: 'registerElement' in document && 'import' in document.createElement('link') && 'content' in document.createElement('template')
}, null, '  '));
Run Code Online (Sandbox Code Playgroud)

  • 在世界的某个地方,一只小猫死于我们实际上并不需要的每一个正则表达式. (7认同)
  • 不幸的是,navigator.vendor === "Google Inc." 在 Opera(至少 v.49)上,因此使用您的代码 Opera 显示为 Chrome。 (2认同)
  • 没有解释,没有关于假阳性/阴性的迹象,只是在这里转储了一段代码......这个回应真的应该被否决。它甚至不是对所问问题的答案。 (2认同)

Jay*_*ing 14

从 Chrome 89(2021 年 3 月)开始,所有早期答案均已过时。Chrome 现在支持用户代理提示。所以现在应该使用以下方法来完成:

navigator.userAgentData?.brands?.some(b => b.brand === 'Google Chrome')
Run Code Online (Sandbox Code Playgroud)

或者,如果您不使用 Babel:

navigator.userAgentData && navigator.userAgentData.brands && navigator.userAgentData.brands.some(b => b.brand === 'Google Chrome')
Run Code Online (Sandbox Code Playgroud)

对于 Chrome 89 及更高版本,此返回 true,对于最新的 Opera 和 Edge,返回 false,对于不支持 userAgentData 的浏览器,返回 undefined。


nav*_*een 8

var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
Run Code Online (Sandbox Code Playgroud)

  • 在Microsoft Edge中返回"true". (14认同)
  • 我喜欢这个,记住你也可以做var is_chrome = /chrome/i.test(navigator.userAgent); 太 (3认同)