How to determine if Chrome browser works with jQuery?

I have a little problem with a function working in chrome that works correctly in Safari, like in webkit browsers ...

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

Unfortunately, I used this to determine if this is a web browser browser:

if ($.browser.webkit) { 

But I need to detect:

 if ($.browser.chrome) { 

Is it possible to write a similar operator (working version above)?

+51
jquery cross-browser google-chrome jquery-ui jquery-selectors
Jun 14 '11 at 5:32
source share
10 answers
 $.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); if($.browser.chrome){ ............ } 

UPDATE: (10x to @Mr. Bacciagalupe)

jQuery removed $.browser from 1.9 and their latest release.

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

+101
Jun 14 2018-11-11T00:
source share
 if(/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())){ alert('I am chrome'); } 
+40
Jul 17 2018-12-12T00:
source share

Endless user is right,

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

It is best to discover a Chrome browser using jQuery.

If you use IE and add GoogleFrame as a plugin, then

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

It will be considered as a Chrome browser, because the GoogleFrame plugin modifies the property of the navigator and adds a chrome frame to it.

+9
Oct. 16
source share

This question has already been discussed here: JavaScript: how do I know if the browser is a Chrome user?

Please try the following:

 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 true for window.chrome

So use below:

 // please note, // that IE11 now returns undefined again for window.chrome // and new Opera 30 outputs true for window.chrome // and new IE Edge outputs to true now for window.chrome // so use the below updated condition var isChromium = window.chrome, vendorName = window.navigator.vendor, isOpera = window.navigator.userAgent.indexOf("OPR") > -1, isIEedge = window.navigator.userAgent.indexOf("Edge") > -1; if(isChromium !== null && isChromium !== undefined && vendorName === "Google Inc." && isOpera == false && isIEedge == false) { // is Google chrome } else { // not Google chrome } 

We recommend using jQuery.browser over messages. But the jQuery API recommends not using this method .. (see DOCS in the API ). And declares that its functionality can be transferred to the plugin supported by the team in a future version of jQuery.

The jQuery API recommends using jQuery.support .

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

Better to use object object detection instead of browser detection.

Also, if you use the Google Chrome Inspector and go to the console tab. Type in a "window" and press "Enter." Then you can view the DOM properties for the "window object". When you destroy an object, you can view all the properties, including the "chrome" property.

Hope this helps, although the question was related to jQuery. But sometimes direct javascript is simpler!

+8
Oct 20
source share

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

 $.browser.chrome = (typeof window.chrome === "object"); 
+5
Jul 08 '12 at 11:33
source share
 var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() ); 
+4
Jun 14 2018-11-11T00:
source share

Although this is not jQuery, I myself use jquery, but I used the script on this page several times to detect the browser. It detects all major browsers, and then some. The work is pretty much all done for you.

+1
Jun 14 '11 at 5:40
source share

Unfortunately, due to the latest update of Opera !!window.chrome (and other tests on the window object), when testing in Opera returns true.

Conditionizr will take care of this for you and solve the Opera problem:

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

I would suggest using it, since none of the above actions is valid.

This allows:

 if (conditionizr.chrome) {...} 

Conditionizr takes care of other browser discoveries and is much faster and more reliable than jQuery hacks.

+1
Dec 04 '13 at
source share

As a quick add, and I'm surprised that no one thought about this, you could use the in operator:

 "chrome" in window 

Obviously, this is not a use of jQuery, but I figured I would put it since it was convenient for times when you are not using any external libraries.

+1
Jun 08 '15 at 18:36
source share

When I test @IE's answer, I am always “true”. The best way is that @IE also works:

 var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); 

As described in this answer: stack overflow

0
May 14 '16 at 13:32
source share



All Articles