6

Possible Duplicate:
Safe feature-based way for detecting Google Chrome with Javascript?
Using Javascript to detect Google Chrome to switch CSS

Is there a statement that I can use with javascript, to only execute a code if the user is using a browser other than google chrome?

Community
  • 1
  • 1
Muhammed Bhikha
  • 4,881
  • 9
  • 36
  • 47

4 Answers4

15
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

if(!is_chrome)
{
//do stuff
}  

Edit: Not sure how this will effect chrome mobile browser.

Second Edit: This throws a false positive in Microsoft Edge, the below code is the updated version:

var is_chrome = (typeof window.chrome === "object" && navigator.appVersion.indexOf('Edge') === -1)
DickieBoy
  • 4,886
  • 1
  • 28
  • 47
9

Sure, i use this javascript some times for checking browser support.

var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/
var IE7 = (document.all && !window.opera && window.XMLHttpRequest && navigator.userAgent.toString().toLowerCase().indexOf('Trident/4.0') == -1) ? true : false;
var IE8 = (navigator.userAgent.toString().toLowerCase().indexOf('trident/4.0') != -1);
var IE9 = (navigator.userAgent.toString().toLowerCase().indexOf('trident/5.0') != -1);
var SAFARI = (navigator.userAgent.toString().toLowerCase().indexOf("safari") != -1) && (navigator.userAgent.toString().toLowerCase().indexOf("chrome") == -1);
var FIREFOX = (navigator.userAgent.toString().toLowerCase().indexOf("firefox") != -1);
var CHROME = (navigator.userAgent.toString().toLowerCase().indexOf("chrome") != -1);
var ANDROID= (navigator.appVersion.toString().indexOf("Android", 0)!=-1)
var MOBILE_SAFARI = ((navigator.userAgent.toString().toLowerCase().indexOf("iphone")!=-1) || (navigator.userAgent.toString().toLowerCase().indexOf("ipod")!=-1) || (navigator.userAgent.toString().toLowerCase().indexOf("ipad")!=-1)) ? true : false;

The in your javascript use the variables with if statements.

Patrick
  • 684
  • 1
  • 9
  • 18
4

Checking for the window.chrome property is a fairly good way to detect Chrome without relying on the user agent string:

if(typeof window.chrome != "object") {
    // not Chrome
} else {
    // it's Chrome (or some fork of Chromium)
}
apsillers
  • 112,806
  • 17
  • 235
  • 239
  • False positive on Edge. – Sky Apr 08 '18 at 19:18
  • @Sky Interesting! I wonder if Edge's `window.chrome` has certain properties on it to distinguish it from Chrome's? I can't test at the moment. – apsillers Apr 08 '18 at 20:25
  • Edge has even the word "chrome" in its user-agent! I manged to use this code finally: `if (typeof window.chrome === "object" && navigator.appVersion.indexOf('Edge') === -1) {alert('chrome')}` – Sky Apr 08 '18 at 20:28
0

While other answers to this question are valid, I would warn you against relying on "browser sniffing" unless absolutely necessary. Check out: http://www.sitepoint.com/why-browser-sniffing-stinks/

Instead, you can use handy tools such as Modernizr to detect whether a client can support individual features.

adamb
  • 4,815
  • 1
  • 25
  • 40