[removed] How to find out if the user browser is Chrome?

后端 未结 15 1534
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 09:41

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

How do I create such functionality?

相关标签:
15条回答
  • 2020-11-22 10:10
    var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
    
    0 讨论(0)
  • 2020-11-22 10:11

    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);
    
    0 讨论(0)
  • 2020-11-22 10:13

    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.

    0 讨论(0)
  • 2020-11-22 10:16

    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, '  '));

    0 讨论(0)
  • 2020-11-22 10:18

    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 !!

    0 讨论(0)
  • 2020-11-22 10:19

    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.

    0 讨论(0)
提交回复
热议问题