jQuery 如何使用jQuery检测浏览器是否是Chrome?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6339480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:43:04  来源:igfitidea点击:

How to detect if a browser is Chrome using jQuery?

jqueryjquery-uigoogle-chromejquery-selectorscross-browser

提问by TaylorMac

I have a bit of an issue with a function running in chrome that works properly in Safari, both webkit browsers...

我有一个在 chrome 中运行的函数在 Safari 中正常工作的问题,两个 webkit 浏览器......

I need to customize a variable in a function for Chrome, but not for Safari.

我需要在 Chrome 的函数中自定义一个变量,而不是 Safari。

Sadly, I have been using this to detect if it is a webkit browser:

可悲的是,我一直在使用它来检测它是否是一个 webkit 浏览器:

if ($.browser.webkit) {

But I need to detect:

但我需要检测:

if ($.browser.chrome) {

Is there any way to write a similar statement (a working version of the one above)?

有没有办法写出类似的声明(上述声明的工作版本)?

回答by Haim Evgi

$.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); 

if($.browser.chrome){
  ............

}

UPDATE:(10x to @Mr. Bacciagalupe)

更新:(10 倍于@Bacciagalupe 先生)

jQuery has removed $.browserfrom 1.9and their latest release.

jQuery的已删除$.browser1.9和他们的最新版本。

But you can still use $.browser as a standalone plugin, found here

但是你仍然可以使用 $.browser 作为一个独立的插件,在这里找到

回答by adedoy

if(/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())){
 alert('I am chrome');
}

回答by Jonathan Marzullo

This question was already discussed here: JavaScript: How to find out if the user browser is Chrome?

这个问题已经在这里讨论过:JavaScript: How to find the user browser is Chrome?

Please try this:

请试试这个:

var isChromium = window.chrome;
if(isChromium){
    // is Google chrome 
} else {
    // not Google chrome 
}

But a more complete and accurate answer would be this since IE11, IE Edge, and Opera will also return truefor window.chrome

但是,一个更加完整和准确的回答会是这样,因为IE11,IE边缘,和Opera也将返回truewindow.chrome

So use the below:

所以使用下面的:

// 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 
}

Above posts advise to use jQuery.browser. But the jQuery API recommends against using this method.. (see DOCS in API). And states its functionality may be moved to a team-supported plugin in a future release of jQuery.

以上帖子建议使用 jQuery.browser。但是 jQuery API 建议不要使用这种方法..(请参阅API 中的 DOCS)。并声明其功能可能会在 jQuery 的未来版本中移至团队支持的插件中。

The jQuery API recommends to use jQuery.support.

jQuery API 建议使用jQuery.support.

The reason being is that 'jQuery.browser' uses the user agent which can be spoofed and it is actually deprecated in later versions of jQuery. If you really want to use $.browser.. Here is the link to the standalone jQuery plugin, since it has been removed from jQuery version 1.9.1. https://github.com/gabceb/jquery-browser-plugin

原因是'jQuery.browser' 使用了可以被欺骗的用户代理,它实际上在 jQuery 的更高版本中被弃用了。如果您真的想使用 $.browser.. 这里是独立 jQuery 插件的链接,因为它已从 jQuery 1.9.1 版中删除。https://github.com/gabceb/jquery-browser-plugin

It's better to use feature object detection instead of browser detection.

最好使用特征对象检测而不是浏览器检测。

Also 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.

此外,如果您使用 Google Chrome 检查器并转到控制台选项卡。输入“窗口”并按回车键。然后您就可以查看“窗口对象”的 DOM 属性。当您折叠对象时,您可以查看所有属性,包括“chrome”属性。

I hope this helps, even though the question was how to do with with jQuery. But sometimes straight javascript is more simple!

我希望这会有所帮助,即使问题是如何使用 jQuery。但有时直接的 javascript 更简单!

回答by Master

User Endless is right,

User Endless 是对的,

$.browser.chrome = (typeof window.chrome === "object"); 

code is best to detect Chrome browser using jQuery.

代码最好使用jQuery检测Chrome浏览器。

If you using IE and added GoogleFrame as plugin then

如果您使用 IE 并添加 GoogleFrame 作为插件,那么

var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );

code will treat as Chrome browser because GoogleFrame plugin modifying the navigator property and adding chromeframe inside it.

代码将被视为 Chrome 浏览器,因为 GoogleFrame 插件修改了 navigator 属性并在其中添加了 chromeframe。

回答by thinkdevcode

var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );

回答by Endless

userAgent can be changed. for more robust, use the global variable specified by chrome

userAgent 可以更改。为了更健壮,请使用由 chrome 指定的全局变量

$.browser.chrome = (typeof window.chrome === "object");

回答by Rafael Soteras

if(navigator.vendor.indexOf('Goog') > -1){
  //Your code here
}

回答by stefgosselin

Although it is not Jquery , I use jquery myself but for browser detection I have used the script on this pagea few times. It detects all major browsers, and then some. The work is pretty much all done for you.

虽然它不是 Jquery ,但我自己使用 jquery 但为了浏览器检测,我已经多次使用此页面上的脚本。它检测所有主要浏览器,然后检测一些. 这项工作几乎都为您完成了。

回答by Halcyon991

Sadly due to Opera's latest update !!window.chrome(and other tests on the window object) when testing in Opera returns true.

可悲的是,由于 Opera 的最新更新!!window.chrome(以及对 window 对象的其他测试),在 Opera 中的测试返回 true 时。

Conditionizr takes care of this for you and solves the Opera issue:

Conditionizr 会为您解决这个问题并解决 Opera 问题:

conditionizr.add('chrome', [], function () {
    return !!window.chrome && !/opera|opr/i.test(navigator.userAgent);
});

I'd highly suggest using it as none of the above are now valid.

我强烈建议使用它,因为以上都不是现在有效的。

This allows you to do:

这允许您执行以下操作:

if (conditionizr.chrome) {...}

Conditionizrtakes care of other browser detects and is much faster and reliable than jQuery hacks.

Conditionizr负责处理其他浏览器检测,并且比 jQuery hacks 更快、更可靠。

回答by Florrie

As a quick addition, and I'm surprised nobody has thought of this, you could use the inoperator:

作为快速补充,我很惊讶没有人想到这一点,您可以使用in运算符:

"chrome" in window

Obviously this isn't using JQuery, but I figured I'd put it since it's handy for times when you aren't using any external libraries.

显然这不是使用 JQuery,但我想我会使用它,因为它在您不使用任何外部库的时候很方便。