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

Tay*_*Mac 56 jquery google-chrome jquery-ui cross-browser jquery-selectors

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

我需要在Chrome的函数中自定义变量,但不能为Safari自定义变量.

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

if ($.browser.webkit) {
Run Code Online (Sandbox Code Playgroud)

但我需要检测:

if ($.browser.chrome) {
Run Code Online (Sandbox Code Playgroud)

有没有办法写一个类似的声明(上面一个的工作版本)?

Hai*_*vgi 106

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

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

}
Run Code Online (Sandbox Code Playgroud)

更新:(10x到@Mr.Bacciagalupe)

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

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

  • Chromium在其User-Agent中还包含"Chrome",但为什么不``/ chrom(e | ium)/`只是为了安全:) (3认同)
  • @Jacob,jQuery从1.9及其最新版本中删除了$ .browser.但你仍然可以使用$ .browser作为一个独立的插件,可以在这里找到:https://github.com/gabceb/jquery-browser-plugin (3认同)
  • 在IE8上失败,因为它列出了chromeframe:mozilla/4.0(兼容; msie 8.0; windows nt 5.1; trident/4.0; chromeframe/25.0.1364.97; .net clr 1.1.4322; .net clr 2.0.50727; infopath.1; ms -rtc lm 8; .net4.0c; .net4.0e; .net clr 3.0.4506.2152; .net clr 3.5.30729) (2认同)

ade*_*doy 46

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


Jon*_*llo 11

这个问题已经在这里讨论了:JavaScript:如何确定用户浏览器是否为Chrome?

请试试这个:

var isChromium = window.chrome;
if(isChromium){
    // is Google chrome 
} else {
    // not Google chrome 
}
Run Code Online (Sandbox Code Playgroud)

但是,由于IE11,IE Edge和Opera也会返回true,因此会有更完整和准确的答案window.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)

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

jQuery API建议使用jQuery.support.

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

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

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

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


小智 9

用户无尽的是对的,

$.browser.chrome = (typeof window.chrome === "object"); 
Run Code Online (Sandbox Code Playgroud)

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

如果您使用IE并添加了GoogleFrame作为插件

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

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


thi*_*ode 5

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


End*_*ess 5

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

$.browser.chrome = (typeof window.chrome === "object");
Run Code Online (Sandbox Code Playgroud)