245

I need some function returning a boolean value to check if the browser is Chrome.

How do I create such functionality?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Rella
  • 59,216
  • 102
  • 341
  • 614
  • 17
    Are you sure that you don't want to do feature detection instead (instead of asking "is this Chrome?" ask "can this do that I need?") – bdukes Dec 30 '10 at 18:20
  • 1
    Amen to that - detecting specific browsers is exactly how we got the problem of sites refusing to work with any other browser than IE and Netscape, even when other browsers are perfectly capable of rendering them properly. Capability detection is the safer, future-compatible, way forward. – kander Dec 30 '10 at 18:41
  • 47
    who knows? he might want to have user download a chrome extension – naveen Dec 30 '10 at 18:53
  • 1
    No - my point is to use some three.js just to create fun 3d box backgrownd=) which works fast only in chrome...=) – Rella Dec 30 '10 at 18:54
  • 2
    This question illustrates the problem of user agent detection. Only three years later, the Fun 3D Box Background will (try to) load in Chrome my low-end mobile phone but won't even try in Firefox in my high-end desktop computer. – Álvaro González Dec 05 '13 at 10:53
  • @ÁlvaroG.Vicario, agreed. The use case given isn't great. I have a situation where we've developed a proof of concept app and know for a fact that it only lays out correctly in Chrome, yet we'll be sending the link out to a bunch of non-technical execs. So for those who click the link and open it in IE/Firefox/Opera, we show a note explaining that this is just a POC and we will make it support their browser in future. I think that's a valid use case. – Drew Noakes Dec 05 '13 at 11:01
  • 5
    I agree feature detection is the way to go. but there are legitimate areas where you would like to detect. e.g. I want to monkey patch xhr.sendAsBinary for chrome only. my initial solution checked if filereader.readasbinary is implemented. however, i am having issues where it patches for certain mobile browsers as well and therefore upload fails. I want this fix only for chrome. – frostymarvelous Dec 15 '13 at 01:08
  • Another example where feature detection will not work: I want to detect whether entering the name of a site and TAB TAB makes it possible to use the site-local search, so I can add a tip. This is not possible to do using feature detection. – user239558 Mar 17 '14 at 07:46
  • 4
    Want to know why it might be relevant to know if a browser is Chrome? How about Chrome not being able to load RSS feeds? So that instead of linking to a RSS feed that will fail to load in Chrome, you could actually provided a warning or redirect the user? No thanks to you Google Chrome... – Pic Mickael Apr 06 '15 at 03:12
  • To those preaching feature detection... yes it's generally understood that it's preferable, but there are many cases where it's not viable, so the question has merit. Eg, polyfilling incomplete/incorrect implementations of a feature such as IndexedDB in IE/Edge. – Hans Feb 16 '17 at 20:14
  • Like @PicMickael I am also looking at this for RSS feed alternative info to the user, since Chrome does not provide any native RSS ability. – eidylon Nov 27 '18 at 16:50
  • 1
    There are odd situations where Google Chrome browser has a specific bug/inconsistency that does not exist in other browsers. I had this few times in past five years or so... – Miloš Đakonović Sep 27 '19 at 13:33
  • I'd like to re-surface this question - is there a reliable & simple answer to this yet? There are many questions like this on SO and yet most of them work off the `userAgent` function which even w3schools acknowledges is inaccurate. I've tested some of the answers to this question and similar ones, and none of them are even 50% reliable. If I'm better off asking a new question please let me know. – 5Diraptor Mar 17 '21 at 16:18
  • @5Diraptor - if you look at Jonathan's answer as Rion's top answer recommends you do, you'll see the history of revision "UPDATE"s -- what this history tells you is that no, there is no reliable and simple answer to this and there probably never will be. If you absolutely must detect _which browser_ instead of using feature detection you are going to have maintenance to keep up with changes. – Stephen P Apr 08 '21 at 18:45

18 Answers18

339

To check if browser is Google Chrome, try this:

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

Example of use: http://codepen.io/jonathan/pen/WpQELR

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.

I hope this helps!

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!

Jonathan Marzullo
  • 6,451
  • 2
  • 38
  • 45
  • This does not work for IE10. typeof window.chrome in IE10 returns {object} – magritte May 30 '13 at 11:00
  • it wouldn't work in ie10 because ie10 does not have window.chrome.. . so this would return false in ie10. – Jonathan Marzullo May 31 '13 at 14:05
  • if NOT chrome it would also return undefined or false – Jonathan Marzullo Jul 19 '13 at 22:41
  • window.chrome didn't returned {object} in my computer. I think it would be VERY weird if window.chrome exists in the window object. So far I believe this is the correct answer. – Arnaldo Capo Nov 25 '13 at 19:50
  • ^ Exists in an IE browser I meant. – Arnaldo Capo Nov 25 '13 at 21:07
  • Worth noting that Opera now responds _true_ to window.chrome! Check out conditionizr.com which has a bulletproof detect + fix. – Stephen Jenkins Dec 04 '13 at 10:37
  • @Halcyon991 Thank you for the heads up! Looks like Opera 18 is now based on Chromium. I added a condition, to also check if the browser is **Google** (Chrome) vs **Opera** (Chrome), Thanks again – Jonathan Marzullo Dec 04 '13 at 19:28
  • 2
    var isGoogleChrome = window.chrome != null && window.navigator.vendor === "Google Inc."; – Ring Jan 22 '14 at 01:30
  • @Mr.Bacciagalupe: I had to use Ring's updated answer to make this work `if(isChromium === true)` does not work in Chrome version 33. – Adrien Be Mar 27 '14 at 13:44
  • @Ring: did you test this solution across browser versions? I could only test on Chrome 33, IE11 & Firefox 27 (latest versions of each as of writting, end of March 2014) – Adrien Be Mar 27 '14 at 13:46
  • @Ring and @Adrien Be .. Thanks.. IE11 will now return true for `window.chrome` ... IE11 also returns a empty string for `window.navigator.vendor`. So if you test for both `window.chrome` and `window.navigator.vendor` then you should be fine. :) – Jonathan Marzullo Mar 27 '14 at 13:59
  • Running IE11, "window.chrome" returns undefined in the console – vogomatix Aug 13 '14 at 16:00
  • @vogomatix Thanks.. 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.. ohh silly Microsoft :) – Jonathan Marzullo Aug 14 '14 at 18:52
  • So we have some builds of IE11 that don't pretend to be chrome and others that do - wonderful! :-) – vogomatix Aug 15 '14 at 14:51
  • @vogomatix .. if you have automatic updates ON for IE and windows 7 or 8, than you should be fine. One of these days Microsoft will get their act together .. or not :) – Jonathan Marzullo Aug 15 '14 at 18:20
  • for the newest version of Opera this check fails. Still getting browser is Chrome on it. :( – Martin Asenov Jul 23 '15 at 08:55
  • Thanks @Martin Asenov .. i just added an additional check to my condition above to check for new Opera 30 due to it using the same Google Chrome engine, and outputing true to window.chrome. The above now detects only Google Chrome :) – Jonathan Marzullo Jul 24 '15 at 11:04
  • this fails for Chrome on iOS because it's safari in a chrome skin I guess; – xinthose Feb 05 '16 at 15:08
  • 1
    Thanks @xinthose .. I just added a check for IOS Chrome.. much appreciated! :) – Jonathan Marzullo Feb 05 '16 at 15:14
  • @DanielWallman What Android version are you using? I tested this on Android 6.1 and it alerts() showing it detects it is Google Chrome .. for Desktop Chrome and Mobile Chrome. See example: http://codepen.io/jonathan/pen/WpQELR – Jonathan Marzullo Feb 28 '17 at 15:26
  • As a side-note, this detects "chromium" as well as Google Chrome, FWIW... – rogerdpack Mar 13 '17 at 23:39
  • @rogerdpack Thank you for notifying me :). When i have time I will have to add a check for `navigator.plugins[i].name == 'Chromium PDF Viewer'` to make sure it is Google Chrome but not 'chromium', based on this SO answer: http://stackoverflow.com/questions/17278770/how-do-i-detect-chromium-specifically-vs-chrome – Jonathan Marzullo Mar 14 '17 at 11:37
  • 2
    Maybe same problem as Daniel Wallman here: my Android Chrome user agent contains the "OPR" string! `Mozilla/5.0 (Linux; Android 8.0.0; ASUS_Z012D Build/OPR1.170623.026) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36`, therefore `isChrome()` returns *false*. – Frosty Z Apr 17 '18 at 21:44
  • 2
    Thank you @FrostyZ and @DanielWallman for letting us know. I fixed it so Opera checks for `window.opr` is not `undefined`. – Jonathan Marzullo Apr 18 '18 at 18:21
  • hmm, I have Opera 50 (Chromium 71) on my Android 7 phone. It returns `undefined` for `window.chrome` and `window.opr`. Any ideas why? – j.j. Feb 24 '19 at 20:05
  • When i console out `window.chrome` or `window.opr` on desktop Opera 58 it outputs those objects, so they do exist but also right after those objects it also outputs `undefined`. Not sure why? You might want to submit a bug and ask the Opera developers why that is? https://bugs.opera.com/wizard/ – Jonathan Marzullo Feb 25 '19 at 14:50
  • Note for TypeScript, you will need to do `const isChromium = (window as any).chrome;` and similar for the Opera check. – Garrett Sep 07 '19 at 22:08
  • It's false positive for Samsung Browser... Here is the UserAgent string: Mozilla/5.0 (Linux; Android 5.0.1; SAMSUNG GT-I9500 Build/LRX22C) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/2.1 Chrome/34.0.1847.76 Mobile Safari/537.36 – yestema Dec 09 '19 at 20:20
  • 5
    The latest Edge user agent value is actually `Edg` and not `Edge` (see also these docs: https://docs.microsoft.com/en-us/microsoft-edge/web-platform/user-agent-string). So maybe this line: `inNav.userAgent.indexOf("Edge")` should be changed to `inNav.userAgent.indexOf("Edg")`. – Wilt Dec 03 '20 at 06:39
221

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.

Community
  • 1
  • 1
Rion Williams
  • 69,631
  • 35
  • 180
  • 307
  • what is the reasoning behind using .toLowerCase - why not just navigator.userAgent.indexOf('Chrome') I see a lot of people use it but im not sure the point? – Jon Jan 30 '13 at 14:47
  • adding .toLowerCase just makes sure the string gets forced to lowercase, so when the string in the condition is checked, it evaluates correctly when checking against the user agent – Jonathan Marzullo Feb 25 '13 at 17:24
  • 7
    @Serg because they do not have Chrome. It is only a skin around iOS Safari. – Poetro Dec 05 '13 at 13:22
  • Strange, sometimes i used to fix bugs only for that skinned with chrome version of ios safari. – Serg Dec 08 '13 at 15:34
  • Just a note - IE9 has "chromeframe" in user agent. – Karel Bílek Mar 06 '14 at 03:33
  • if the browser is Maxthon it will return true also so check first if the browser is not maxthon and then check is it chrome – Marwan Jan 27 '15 at 10:17
  • 2
    Thanks, though your var name should be camelCase – Dimitri Kopriwa Jan 28 '15 at 13:29
  • Feature detection is great, except when developing extensions for sites that return completely different views for Chrome than any other browser. Something that feature detection is no help with. – Craig Brett Feb 22 '15 at 14:28
  • do not use @JonathanMarzullo suggestion anymore, in 2015 every browser has a window.chrome set :-( – artfulhacker Oct 13 '15 at 17:15
  • @artfulhacker .. IE will output `undefined` for `windows.chrome`.. if you even bothered to look at my full answer below you would see i account for that based on the vendor name, try looking at the condition in my statement first before badgering someones answer :) – Jonathan Marzullo Oct 13 '15 at 18:01
  • @artfulhacker Stack Overflow is not the community for badgering people. IE11 outputs `window.chrome` as `undefined` .. so i will add a check for IE Edge, Thank You.. At least have some common courtesy like everyone else and leave a helpful comment instead of being rude :) – Jonathan Marzullo Oct 13 '15 at 18:19
  • @artfulhacker My comment above was written in 2012, when IE Edge did not exist. Please see my full answer below. Every time i am notified or find out that a modern browser has changed, I update my answer below! But thank you for letting me know about IE Edge.outputting true for window.chrome :) – Jonathan Marzullo Oct 13 '15 at 18:58
  • @JonathanMarzullo you're welcome, I even up voted the answer :-) maybe time to remove the comment since it can't be updated. – artfulhacker Oct 13 '15 at 18:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92180/discussion-between-artfulhacker-and-jonathan-marzullo). – artfulhacker Oct 13 '15 at 19:07
  • 3
    Returns `true` in Microsoft Edge. – Cobby Feb 01 '16 at 02:26
  • 5
    Since a lot of browser returns true at this, here's the code I used which excluded Edge, Maxthon, iOS safari ...etc `var is_chrome = ((navigator.userAgent.toLowerCase().indexOf('chrome') > -1) &&(navigator.vendor.toLowerCase().indexOf("google") > -1));` – Alex C. Mar 01 '16 at 16:09
  • 2
    Opera (at least version 42) returns `Google Inc` to `navigator.vendor`, so this method is not bulletproof, something like `/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor) && !/OPR/.test(navigator.userAgent)` would probably work better – yorch Jan 16 '17 at 21:48
  • take care that first part of check will return false on ios chrome – strix25 Apr 27 '21 at 15:17
21

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.

And you can probably skip !!

Simon B.
  • 2,013
  • 18
  • 28
Drew Noakes
  • 266,361
  • 143
  • 616
  • 705
  • 6
    Opera actually returns `true` to `window.chrome`. Check out conditionizr.com which has a bulletproof detect + fix. – Stephen Jenkins Dec 07 '13 at 19:43
  • 7
    Well, Opera is basically Chrome though – Karel Bílek Mar 06 '14 at 03:34
  • I just don't understand why two times !! , u can directly use , if(chrome){ } – Vishal Sharma Aug 06 '14 at 15:11
  • 3
    @vishalsharma, the `!!` converts the value to be either `true` or `false`. `typeof(window.chrome)` gives `"object"`, whereas `typeof(!!window.chrome)` gives `"boolean"`. Your code sample also works too because the `if` statement does the conversion. – Drew Noakes Aug 06 '14 at 20:41
  • yes that was i m trying to tell , *if* will take care of conversion.. yes but u r also right , for assignment we have to use this way.. – Vishal Sharma Aug 07 '14 at 18:43
  • As of today, this fails with Chrome 31 (latest) on iOS 7, iPhone 4 – AnthumChris Mar 24 '15 at 17:24
  • @Chris it fails on iOS because Blink can't be used, WebKit is used here so it would need to fallback to the [user agent](https://developer.chrome.com/multidevice/user-agent). However, you probably wouldn't want to consider this Google Chrome from a support standpoint since it's WebKit, not Blink. – Daniel Imms Nov 19 '15 at 19:49
  • 3
    This also returns `true` for Edge. – Carrm Sep 11 '19 at 13:38
  • This is returning true in IE for me. IE keeps returning exact same as Chrome of all userAgent and Navigator vars. – Sam May 12 '20 at 22:41
  • @Sam are you talking about IE or Edge? Edge is based on Chrome, which returns true for this property. – Drew Noakes May 13 '20 at 03:59
20

even shorter: var is_chrome = /chrome/i.test( navigator.userAgent );

12

console.log(JSON.stringify({
  isAndroid: /Android/.test(navigator.userAgent),
  isCordova: !!window.cordova,
  isEdge: /Edge/.test(navigator.userAgent),
  isFirefox: /Firefox/.test(navigator.userAgent),
  isChrome: /Google Inc/.test(navigator.vendor),
  isChromeIOS: /CriOS/.test(navigator.userAgent),
  isChromiumBased: !!window.chrome && !/Edge/.test(navigator.userAgent),
  isIE: /Trident/.test(navigator.userAgent),
  isIOS: /(iPhone|iPad|iPod)/.test(navigator.platform),
  isOpera: /OPR/.test(navigator.userAgent),
  isSafari: /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent),
  isTouchScreen: ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch,
  isWebComponentsSupported: 'registerElement' in document && 'import' in document.createElement('link') && 'content' in document.createElement('template')
}, null, '  '));
Josef Ježek
  • 1,831
  • 2
  • 14
  • 9
  • 2
    Unfortunately, navigator.vendor === "Google Inc." on Opera (at least v.49) so using your code Opera appears as Chrome. – Wojtek Majerski Dec 04 '17 at 00:56
  • 13
    Somewhere in the world a kitten dies for every regex we don't actually need. – Sz. Mar 12 '18 at 17:10
  • 2
    No explanations, no indications on false positive/negatives, just a piece of code dumped here... This response should really be downvoted. It isn't even an answer to THE question asked. – Alexandre Germain May 26 '19 at 07:30
  • Unfortunately, `navigator.vendor === "Google Inc."` on Edge as well (at least v.89) so using your code Edge also appears as Chrome and `isEdge` becomes false (user agent for Chromium based Edge browser is `Edg`). – Wilt Apr 07 '21 at 06:38
7
var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
naveen
  • 48,336
  • 43
  • 154
  • 235
3

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.

Simon B.
  • 2,013
  • 18
  • 28
MrOodles
  • 1,748
  • 17
  • 21
3

You can use:

navigator.userAgent.indexOf("Chrome") != -1

It is working on v.71

magg89
  • 39
  • 3
1

To check if browser is Google Chrome:

var isChrome = navigator.userAgent.includes("Chrome") && navigator.vendor.includes("Google Inc");

console.log(navigator.vendor);
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36 "

console.log(navigator.userAgent); 
// "Google Inc."
Haikel
  • 613
  • 2
  • 7
  • 12
1

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 /Chrome|CriOS/.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';
}

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.

Wilt
  • 33,082
  • 11
  • 129
  • 176
0

User could change user agent . Try testing for webkit prefixed property in style object of body element

if ("webkitAppearance" in document.body.style) {
  // do stuff
}
guest271314
  • 1
  • 10
  • 82
  • 156
0

Works for me on Chrome on Mac. Seems to be or simpler or more reliable (in case userAgent string tested) than all above.

        var isChrome = false;
        if (window.chrome && !window.opr){
            isChrome = true;
        }
        console.log(isChrome);
yurin
  • 3,512
  • 1
  • 29
  • 29
  • 2
    `const isChrome = window.chrome && !window.opr;` – ifbamoq Feb 13 '20 at 17:01
  • I guess, that answer downvoted by oneliners lovers. Despite, I understand your passion, I don't think you should downvote a perfectly correct answer based just on it. – yurin Jul 25 '20 at 19:12
0

To know the names of different desktop browsers (Firefox, IE, Opera, Edge, Chrome). Except Safari.

function getBrowserName() {
  var browserName = '';
  var userAgent = navigator.userAgent;
  (typeof InstallTrigger !== 'undefined') && (browserName = 'Firefox');
  ( /* @cc_on!@*/ false || !!document.documentMode) && (browserName = 'IE');
  (!!window.chrome && userAgent.match(/OPR/)) && (browserName = 'Opera');
  (!!window.chrome && userAgent.match(/Edge/)) && (browserName = 'Edge');
  (!!window.chrome && !userAgent.match(/(OPR|Edge)/)) && (browserName = 'Chrome');

  /**
   * Expected returns
   * Firefox, Opera, Edge, Chrome
   */
  return browserName;
}

Works in the following browser versions:

Opera - 58.0.3135.79
Firefox - 65.0.2 (64-bit)
IE - 11.413.15063 (JS Fiddle no longer supports IE just paste in Console)
Edge - 44.17763.1.0
Chrome - 72.0.3626.121 (Official Build) (64-bit)

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

iamdevlinph
  • 1,307
  • 1
  • 15
  • 37
0

The best solution I found, and does give either true or false in most browsers is:

var isChrome = (navigator.userAgent.indexOf("Chrome") != -1 && navigator.vendor.indexOf("Google Inc") != -1)

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.

Gefilte Fish
  • 1,131
  • 1
  • 13
  • 14
0

As of Chrome 89 (March 2021), all earlier answers are obsolete. Chrome now supports User Agent Hints. So now this should be done using:

navigator.userAgentData?.brands?.some(b => b.brand === 'Google Chrome')

Or, if you're not using Babel:

navigator.userAgentData && navigator.userAgentData.brands && navigator.userAgentData.brands.some(b => b.brand === 'Google Chrome')

This returns true for Chrome 89 and above, false for the latest Opera and Edge, and undefined for browsers that don't support userAgentData.

Jay Dunning
  • 171
  • 2
  • 5
-1

Check this: How to detect Safari, Chrome, IE, Firefox and Opera browser?

In your case:

var isChrome = (window.chrome.webstore || window.chrome.runtime) && !!window.chrome;
dstronczak
  • 2,226
  • 4
  • 23
  • 41
  • Does not work on Chrome for Android neither in the browser or as PWA. Inspecting dev console shows that window.chrome is `{loadTimes: ƒ, csi: ƒ}` – Simon B. Oct 05 '20 at 08:34
-2
var is_chrome = browseris.chrome

or check ather browsers:

browseris.firefox
browseris.ie
browseris.safari

and olso you can check the version like browseris.chrome7up and etc.

check all existing information in the 'browseris' object

WantToDo
  • 182
  • 1
  • 3
  • 8
-4

all answers are wrong. "Opera" and "Chrome" are same in all cases.

(edited part)

here is the right answer

if (window.chrome && window.chrome.webstore) {
    // this is Chrome
}