// 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 isFirefox = winNav.userAgent.indexOf("Firefox") > -1;
var isIEedge = winNav.userAgent.indexOf("Edg") > -1;
var isIOSChrome = winNav.userAgent.match("CriOS");
var isGoogleChrome = (typeof winNav.userAgentData !== "undefined") ? winNav.userAgentData.brands[2].brand === "Google Chrome" : vendorName === "Google Inc.";
if (isIOSChrome) {
// is Google Chrome on IOS
} else if(
isChromium !== null &&
typeof isChromium !== "undefined" &&
vendorName === "Google Inc." &&
isOpera === false &&
isIEedge === false &&
isGoogleChrome
) {
// is Google Chrome
} else {
// not Google Chrome
}
感谢Halcyon991在下面指出,新的 Opera 18+ 也输出对于 window.chrome 为 true。看起来 Opera 18 是基于 Chromium 31 的。因此,我添加了一项检查,以确保 window.navigator.vendor 是:“Google Inc”,而不是“Opera Software ASA”。还要感谢 Ring 和 Adrien Be 关于 Chrome 33 不再返回 true 的提示...window.chrome 现在检查是否为 null。但请密切关注 IE11,我添加了对 undefined 的检查,因为 IE11 现在输出 undefined,就像首次发布时一样。然后在进行一些更新构建后,它输出到true ..现在最近的更新版本再次输出undefined。微软拿不定主意!
更新 2015 年 7 月 24 日 - Opera 检查
Opera 30 的新增内容刚刚发布。它不再输出window.opera。并且在新的 Opera 30 中 window.chrome 输出为 true。因此您必须检查 OPR 是否在 userAgent 中。我更新了上面的条件以考虑 Opera 30 中的这一新变化,因为它使用与 Google Chrome 相同的渲染引擎。
编辑了 Opera 检查,检查 window.opr 不是未定义,因为现在 Chrome 66 已< window.navigator.vendor 中的代码>OPR。感谢 Frosty Z 和 丹尼尔·沃尔曼举报了这一点!
更新 2024 年 6 月 10 日 - Google Chrome 浏览器品牌检查的更改
添加了额外检查,因为 Google 在 中添加了 window.navigator.userAgentData.brands 数组useragent
更新 2024 年 7 月 18 日 - Google Chrome 浏览器品牌检查更改
修改 Google Chrome 检查 (isGoogleChrome) 以考虑 userAgentData 品牌数组发生变化,并且如果 userAgentData 不存在于非 Google Chrome 浏览器中,则不会触发 undefined。还添加了不 Firefox 检查。
To check if browser is Google Chrome, try this (updated 7/18/2024):
// 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 isFirefox = winNav.userAgent.indexOf("Firefox") > -1;
var isIEedge = winNav.userAgent.indexOf("Edg") > -1;
var isIOSChrome = winNav.userAgent.match("CriOS");
var isGoogleChrome = (typeof winNav.userAgentData !== "undefined") ? winNav.userAgentData.brands[2].brand === "Google Chrome" : vendorName === "Google Inc.";
if (isIOSChrome) {
// is Google Chrome on IOS
} else if(
isChromium !== null &&
typeof isChromium !== "undefined" &&
vendorName === "Google Inc." &&
isOpera === false &&
isIEedge === false &&
isGoogleChrome
) {
// is Google Chrome
} else {
// not Google Chrome
}
The reason this works is because if you use the Google Chrome inspector and go to the console tab. Type 'window' and press enter. Then you be able to view the DOM properties for the 'window object'. When you collapse the object you can view all the properties, including the 'chrome' property.
You can't use strictly equals true anymore to check in IE for window.chrome. IE used to return undefined, now it returns true. But guess what, IE11 now returns undefined again. IE11 also returns a empty string "" for window.navigator.vendor.
UPDATE:
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!
UPDATE 7/24/2015 - addition for Opera check
Opera 30 was just released. It no longer outputs window.opera. And also window.chrome outputs to true in the new Opera 30. So you must check if OPR is in the userAgent. I updated my condition above to account for this new change in Opera 30, since it uses same render engine as Google Chrome.
UPDATE 10/13/2015 - addition for IE check
Added check for IE Edge due to it outputting true for window.chrome .. even though IE11 outputs undefined for window.chrome. Thanks to artfulhacker for letting us know about this!
UPDATE 2/5/2016 - addition for iOS Chrome check
Added check for iOS Chrome check CriOS due to it outputting true for Chrome on iOS. Thanks to xinthose for letting us know about this!
UPDATE 4/18/2018 - change for Opera check
Edited check for Opera, checking window.opr is not undefined since now Chrome 66 has OPR in window.navigator.vendor. Thanks to Frosty Z and Daniel Wallman for reporting this!
UPDATE 6/10/2024 - change for Google Chrome browser brand check
Added extra check for if Google Chrome since Google added window.navigator.userAgentData.brands array in useragent
UPDATE 7/18/2024 - change for Google Chrome browser brand check
Modified if Google Chrome check (isGoogleChrome) to account for userAgentData brands array change and if userAgentData does not existing in non google Chrome Browsers so it doesnt trigger undefined. Also added not Firefox check.
Update: Please see Jonathan's answer for an updated way to handle this. The answer below may still work, but it could likely trigger some false positives in other browsers.
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
However, as mentioned User Agents can be spoofed so it is always best to use feature-detection (e.g. Modernizer) when handling these issues, as other answers mention.
If you want to detect Chrome's rendering engine (so not specific features in Google Chrome or Chromium), a simple option is:
var isChrome = !!window.chrome;
NOTE: this also returns true for many versions of Edge, Opera, etc that are based on Chrome (thanks @Carrm for pointing this out). Avoiding that is an ongoing battle (see window.opr below) so you should ask yourself if you're trying to detect the rendering engine (used by almost all major modern browsers in 2020) or some other Chrome (or Chromium?) -specific feature.
If you're feeling brave, you can experiment with browser sniffing and get a version:
var ua = navigator.userAgent;
if(/chrome/i.test(ua)) {
var uaArray = ua.split(' ')
, version = uaArray[uaArray.length - 2].substr(7);
}
This detected version might be a Chrome version, or a Edge version, or something else. Browser plugins can easily change userAgent and platform and other things though, so this is not recommended.
Apologies to The Big Lebowski for using his answer within mine.
There are some optional window properties that that can be used when doing browser detection. One of them is the optional chrome property (Chromium) and the other the optional opr property (Opera).
If a browser has the optional chrome property on the Window object, it means the browser is a Chromium browser. Previously this meant Chrome in most cases, but these days many browsers are built on Chromium (including Edge and Opera) so only checking the presence of the property will not help to detect Chrome browsers specifically.
Then there are often several user-agents for different browser versions (Edg or Edge) or operation systems (EdgiOS, ChriOS and FxiOS).
I use the following logic and tested against a lot of cases (common user agents):
const GOOGLE_VENDOR_NAME = 'Google Inc.';
function isOpera(){
return Boolean(window.opr);
}
function isChromium() {
return Boolean(window.chrome);
}
function getBrowserName() {
const userAgent = window.navigator.userAgent;
const vendor = window.navigator.vendor;
switch (true) {
case /Edge|Edg|EdgiOS/.test(userAgent):
return 'Edge';
case /OPR|Opera/.test(userAgent) && isOpera():
return 'Opera';
case /CriOS/.test(userAgent):
case /Chrome/.test(userAgent) && vendor === GOOGLE_VENDOR_NAME && isChromium():
return 'Chrome';
case /Vivaldi/.test(userAgent):
return 'Vivaldi';
case /YaBrowser/.test(userAgent):
return 'Yandex';
case /Firefox|FxiOS/.test(userAgent):
return 'Firefox';
case /Safari/.test(userAgent):
return 'Safari';
case /MSIE|Trident/.test(userAgent):
return 'Internet Explorer';
default:
return 'Unknown';
}
}
function isChrome() {
const name = getBrowserName();
return name === 'Chrome';
}
The trick is to test against other browsers then Chrome (Edge, Opera) first. In all these cases in the switch the different possible identifiers for a browser are combined in one regular expression and tested against the user agent string. For Chrome and Opera additional tests for the window property are added and for Chrome we also check whether the vendor name matches the expected value.
Note:I tested against a lot of different user agents, but won't claim here that this solution is flawless. Any suggestions for improvements, or failing browser detections are welcome so I can further improve this code.
UPDATE:
Fixed bug with Chrome on iOS (user agent CriOS) detection. Chrome on iOS doesn't have the chrome: true property on the window object, so should only be tested for presence of the user agent string.
The original code snippet no longer worked for Chrome and I forgot where I found it. It had safari before but I no longer have access to safari so I cannot verify anymore.
Only the Firefox and IE codes were part of the original snippet.
The checking for Opera, Edge, and Chrome is straight forward. They have differences in the userAgent. OPR only exists in Opera. Edge only exists in Edge. So to check for Chrome these string shouldn't be there.
As for the Firefox and IE, I cannot explain what they do.
Using .indexOf instead of .includes makes it more browser-compatible. Even though (or because) the whole point is to make your code browser-specific, you need the condition to work in most (or all) browsers.
发布评论
评论(19)
要检查浏览器是否为 Google Chrome,请尝试以下操作(更新于 7/18/2024):
使用示例:https://codepen.io/jonathan/pen/RwQXZxJ?editors=1111
之所以有效,是因为如果您使用 Google Chrome 检查器并转到控制台选项卡。输入“窗口”并按 Enter 键。然后您就可以查看“窗口对象”的 DOM 属性。当您折叠对象时,您可以查看所有属性,包括“chrome”属性。
您不能再使用严格等于 true 来检查 IE 中的
window.chrome
。 IE 过去返回undefined
,现在返回true
。 但猜猜看,IE11 现在再次返回 undefined。 IE11 还为window.navigator.vendor
返回一个空字符串""
。更新:
感谢Halcyon991在下面指出,新的 Opera 18+ 也输出对于
window.chrome
为 true。看起来 Opera 18 是基于 Chromium 31 的。因此,我添加了一项检查,以确保window.navigator.vendor
是:“Google Inc”
,而不是“Opera Software ASA”
。还要感谢 Ring 和 Adrien Be 关于 Chrome 33 不再返回 true 的提示...window.chrome
现在检查是否为 null。但请密切关注 IE11,我添加了对undefined
的检查,因为 IE11 现在输出undefined
,就像首次发布时一样。然后在进行一些更新构建后,它输出到true
..现在最近的更新版本再次输出undefined
。微软拿不定主意!更新 2015 年 7 月 24 日 - Opera 检查
Opera 30 的新增内容刚刚发布。它不再输出
window.opera
。并且在新的 Opera 30 中window.chrome
输出为 true。因此您必须检查 OPR 是否在 userAgent 中。我更新了上面的条件以考虑 Opera 30 中的这一新变化,因为它使用与 Google Chrome 相同的渲染引擎。更新 10/13/2015 - 添加 IE 检查
添加了对 IE Edge 的检查,因为它为
window.chrome
输出true
.. IE11 为window.chrome
输出undefined
。感谢 artfulhacker 让我们知道这一点!更新 2/5/2016 - 添加了 iOS Chrome 检查
添加了 iOS Chrome 检查
CriOS
的检查,因为它为 iOS 上的 Chrome 输出true
。感谢 xinthose 让我们知道这一点!更新 2018 年 4 月 18 日 - Opera 检查的更改
编辑了 Opera 检查,检查
window.opr
不是未定义
,因为现在 Chrome 66 已<window.navigator.vendor
中的代码>OPR。感谢 Frosty Z 和 丹尼尔·沃尔曼举报了这一点!更新 2024 年 6 月 10 日 - Google Chrome 浏览器品牌检查的更改
添加了额外检查,因为 Google 在
中添加了
window.navigator.userAgentData.brands
数组useragent更新 2024 年 7 月 18 日 - Google Chrome 浏览器品牌检查更改
修改 Google Chrome 检查 (
isGoogleChrome
) 以考虑userAgentData 品牌数组发生变化,并且如果
userAgentData
不存在于非 Google Chrome 浏览器中,则不会触发undefined
。还添加了不 Firefox 检查。To check if browser is Google Chrome, try this (updated 7/18/2024):
Example of use: https://codepen.io/jonathan/pen/RwQXZxJ?editors=1111
The reason this works is because if you use the Google Chrome inspector and go to the console tab. Type 'window' and press enter. Then you be able to view the DOM properties for the 'window object'. When you collapse the object you can view all the properties, including the 'chrome' property.
You can't use strictly equals true anymore to check in IE for
window.chrome
. IE used to returnundefined
, now it returnstrue
. But guess what, IE11 now returns undefined again. IE11 also returns a empty string""
forwindow.navigator.vendor
.UPDATE:
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 thewindow.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 forundefined
since IE11 now outputsundefined
, like it did when first released.. then after some update builds it outputted totrue
.. now recent update build is outputtingundefined
again. Microsoft can't make up it's mind!UPDATE 7/24/2015 - addition for Opera check
Opera 30 was just released. It no longer outputs
window.opera
. And alsowindow.chrome
outputs to true in the new Opera 30. So you must check if OPR is in the userAgent. I updated my condition above to account for this new change in Opera 30, since it uses same render engine as Google Chrome.UPDATE 10/13/2015 - addition for IE check
Added check for IE Edge due to it outputting
true
forwindow.chrome
.. even though IE11 outputsundefined
forwindow.chrome
. Thanks to artfulhacker for letting us know about this!UPDATE 2/5/2016 - addition for iOS Chrome check
Added check for iOS Chrome check
CriOS
due to it outputtingtrue
for Chrome on iOS. Thanks to xinthose for letting us know about this!UPDATE 4/18/2018 - change for Opera check
Edited check for Opera, checking
window.opr
is notundefined
since now Chrome 66 hasOPR
inwindow.navigator.vendor
. Thanks to Frosty Z and Daniel Wallman for reporting this!UPDATE 6/10/2024 - change for Google Chrome browser brand check
Added extra check for if Google Chrome since Google added
window.navigator.userAgentData.brands
array inuseragent
UPDATE 7/18/2024 - change for Google Chrome browser brand check
Modified if Google Chrome check (
isGoogleChrome
) to account foruserAgentData
brands array change and ifuserAgentData
does not existing in non google Chrome Browsers so it doesnt triggerundefined
. Also added not Firefox check.更新:请参阅Jonathan 的回答,了解处理此问题的更新方法。下面的答案可能仍然有效,但它可能会在其他浏览器中触发一些误报。
但是,如上所述,用户代理可能会被欺骗,因此最好使用功能检测(例如 Modernizer)在处理这些问题时,正如其他答案提到的那样。
Update: Please see Jonathan's answer for an updated way to handle this. The answer below may still work, but it could likely trigger some false positives in other browsers.
However, as mentioned User Agents can be spoofed so it is always best to use feature-detection (e.g. Modernizer) when handling these issues, as other answers mention.
如果您想检测 Chrome 的渲染引擎(因此不是 Google Chrome 或 Chromium 中的特定功能),一个简单的选项是:
注意:对于 Edge、Opera 等的许多版本,这也会返回
true
是基于 Chrome 的(感谢 @Carrm 指出了这一点)。 避免这种情况是一场持续的战斗(请参阅下面的window.opr
),因此您应该问问自己是否正在尝试检测渲染引擎(2020 年几乎所有主要现代浏览器都使用)或其他一些 Chrome(或 Chromium?)特定功能。和你可以跳过
!!
If you want to detect Chrome's rendering engine (so not specific features in Google Chrome or Chromium), a simple option is:
NOTE: this also returns
true
for many versions of Edge, Opera, etc that are based on Chrome (thanks @Carrm for pointing this out). Avoiding that is an ongoing battle (seewindow.opr
below) so you should ask yourself if you're trying to detect the rendering engine (used by almost all major modern browsers in 2020) or some other Chrome (or Chromium?) -specific feature.And you can probably skip
!!
更短: var is_chrome = /chrome/i.test( navigator.userAgent );
even shorter:
var is_chrome = /chrome/i.test( navigator.userAgent );
从 Chrome 89(2021 年 3 月)开始,所有早期答案均已过时。 Chrome 现在支持用户代理提示。所以现在应该使用以下方法来完成:
或者,如果您不使用 Babel:
这对于 Chrome 89 及更高版本返回 true,对于最新的 Opera 和 Edge 返回 false,对于不支持 userAgentData 的浏览器返回 undefined。
As of Chrome 89 (March 2021), all earlier answers are obsolete. Chrome now supports User Agent Hints. So now this should be done using:
Or, if you're not using Babel:
This returns true for Chrome 89 and above, false for the latest Opera and Edge, and undefined for browsers that don't support userAgentData.
var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );~
这是一个旧答案,写于 12 多年前;截至目前(即 2022 年 8 月)
更新(2022 年 8 月):
如今,使用用户代理(用户代理嗅探)进行浏览器检测通常是一个坏主意。 特征检测应该是正确的选择。
参考:使用用户代理进行浏览器检测
但是,如果您仍然希望这样做,使用第三方库,例如
上面的代码片段使用库 ua-parser-js
PS:请注意,UA 嗅探是不好的尽可能练习和使用特征检测。
var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );~
This is an old answer, written more than 12 years back; as of now(i.e., August 2022)
Update(August 2022):
Nowadays, browser detection using user agent (user agent sniffing) is usually a bad idea. Feature detection should be the way to go.
Reference: Browser detection using the user agent
However, if you still wish to do so, use a third party library like
The above snippet use the library ua-parser-js
P.S: Please note that UA sniffing is bad practice and use feature detection wherever possible.
您可以使用:
它正在 v.71 上运行
You can use:
It is working on v.71
如果您有勇气,可以尝试浏览器嗅探并获取版本:
检测到的版本可能是 Chrome 版本、Edge 版本或其他版本。不过,浏览器插件可以轻松更改 userAgent 和平台等其他内容,因此不建议这样做。
向大勒博斯基道歉,因为他在我的答案中使用了他的答案。
If you're feeling brave, you can experiment with browser sniffing and get a version:
This detected version might be a Chrome version, or a Edge version, or something else. Browser plugins can easily change userAgent and platform and other things though, so this is not recommended.
Apologies to The Big Lebowski for using his answer within mine.
在进行浏览器检测时可以使用一些可选的窗口属性。其中之一是可选的 chrome 属性 (Chromium),另一个是可选的 opr 属性 (Opera)。
如果浏览器在 Window 对象上具有可选的 chrome 属性,则表示该浏览器是 Chromium 浏览器。以前在大多数情况下这意味着 Chrome,但现在许多浏览器都是基于 Chromium(包括 Edge 和 Opera)构建的,因此仅检查该属性的存在无助于专门检测 Chrome 浏览器。
然后,通常有多个针对不同浏览器版本(Edg 或 Edge)或操作系统(EdgiOS、ChriOS 和 FxiOS)的用户代理。
我使用以下逻辑并针对很多情况进行了测试(常见用户代理):
您可以在 中找到此简化代码this fiddle:
技巧是首先针对其他浏览器进行测试,然后是 Chrome(Edge、Opera)。在所有这些情况下,在交换机中,浏览器的不同可能标识符被组合在一个正则表达式中,并针对用户代理字符串进行测试。对于 Chrome 和 Opera,添加了针对 window 属性的附加测试,对于 Chrome,我们还检查供应商名称是否与预期值匹配。
注意: 我针对许多不同的用户代理进行了测试,但不会在此声称此解决方案是完美的。欢迎提出任何改进建议或失败的浏览器检测,以便我可以进一步改进此代码。
更新:
修复了 iOS 上的 Chrome(用户代理 CriOS)检测的错误。 iOS 上的 Chrome 在窗口对象上没有
chrome: true
属性,因此只应测试用户代理字符串是否存在。There are some optional window properties that that can be used when doing browser detection. One of them is the optional
chrome
property (Chromium) and the other the optionalopr
property (Opera).If a browser has the optional
chrome
property on the Window object, it means the browser is a Chromium browser. Previously this meant Chrome in most cases, but these days many browsers are built on Chromium (including Edge and Opera) so only checking the presence of the property will not help to detect Chrome browsers specifically.Then there are often several user-agents for different browser versions (Edg or Edge) or operation systems (EdgiOS, ChriOS and FxiOS).
I use the following logic and tested against a lot of cases (common user agents):
You can find this simplified code in this fiddle:
The trick is to test against other browsers then Chrome (Edge, Opera) first. In all these cases in the switch the different possible identifiers for a browser are combined in one regular expression and tested against the user agent string. For Chrome and Opera additional tests for the window property are added and for Chrome we also check whether the vendor name matches the expected value.
Note: I tested against a lot of different user agents, but won't claim here that this solution is flawless. Any suggestions for improvements, or failing browser detections are welcome so I can further improve this code.
UPDATE:
Fixed bug with Chrome on iOS (user agent CriOS) detection. Chrome on iOS doesn't have the
chrome: true
property on the window object, so should only be tested for presence of the user agent string.我可以在 Mac 上的 Chrome 上使用。似乎比上述所有方法更简单或更可靠(如果测试了 userAgent 字符串)。
Works for me on Chrome on Mac. Seems to be or simpler or more reliable (in case userAgent string tested) than all above.
要检查浏览器是否为 Google Chrome:
To check if browser is Google Chrome:
用户可以更改用户代理。尝试测试
body
元素的style
对象中的webkit
前缀属性User could change user agent . Try testing for
webkit
prefixed property instyle
object ofbody
element了解不同桌面浏览器的名称(Firefox、IE、Opera、Edge、Chrome)。除了Safari。
适用于以下浏览器版本:
此处查看要点和小提琴此处
原始代码片段不再适用于 Chrome,我忘记了在哪里找到它。之前有 safari,但我无法再访问 safari,所以我无法再验证。
只有 Firefox 和 IE 代码是原始代码片段的一部分。
对 Opera、Edge 和 Chrome 的检查非常简单。它们在 userAgent 上有区别。
OPR
仅存在于 Opera 中。Edge
仅存在于 Edge 中。因此,要检查 Chrome,这些字符串不应该存在。至于Firefox和IE,我无法解释它们的作用。
我将将此功能添加到我正在编写的包
To know the names of different desktop browsers (Firefox, IE, Opera, Edge, Chrome). Except Safari.
Works in the following browser versions:
View the gist here and the fiddle here
The original code snippet no longer worked for Chrome and I forgot where I found it. It had safari before but I no longer have access to safari so I cannot verify anymore.
Only the Firefox and IE codes were part of the original snippet.
The checking for Opera, Edge, and Chrome is straight forward. They have differences in the userAgent.
OPR
only exists in Opera.Edge
only exists in Edge. So to check for Chrome these string shouldn't be there.As for the Firefox and IE, I cannot explain what they do.
I'll be adding this functionality to a package i'm writing
我发现的最好的解决方案(在大多数浏览器中给出 true 或 false)是:
使用
.indexOf
而不是.includes
使其与浏览器更加兼容。尽管(或因为)重点是使您的代码特定于浏览器,但您需要在大多数(或所有)浏览器中工作的条件。
The best solution I found, and does give either true or false in most browsers is:
Using
.indexOf
instead of.includes
makes it more browser-compatible.Even though (or because) the whole point is to make your code browser-specific, you need the condition to work in most (or all) browsers.
现在你可以使用 navigator.userAgent.includes("Chrome")
now u can use navigator.userAgent.includes("Chrome")
检查这个:如何检测 Safari、Chrome、 IE、Firefox 和 Opera 浏览器?
在您的情况下:
Check this: How to detect Safari, Chrome, IE, Firefox and Opera browser?
In your case:
或者检查其他浏览器:
也可以检查版本,例如
browseris.chrome7up
等。检查“browseris”对象中的所有现有信息
or check ather browsers:
and olso you can check the version like
browseris.chrome7up
and etc.check all existing information in the 'browseris' object
所有的答案都是错误的。 “Opera”和“Chrome”在所有情况下都是相同的。
(编辑部分)
这是正确的答案
all answers are wrong. "Opera" and "Chrome" are same in all cases.
(edited part)
here is the right answer