Javascript's way of judging the chrome browser

  • 2020-03-30 02:26:09
  • OfStack

Check whether the string returned by the userAgent contains "Chrome" by the userAgent judgment, and how to check it is done by the method indexOf.


<script type="text/javascript">
  var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
  alert(isChrome);
  if (isChrome) {
    alert(" is Chrome The browser ");
  } else {
    alert(" not Chrome The browser ");
  }
</script>

About indexOf method:

The indexOf method returns an integer value indicating the starting position of the substring inside the String object. That is, the position of the character contained in the indexOf() bracket in the string, returns the number at the position, counting from 0. If a duplicate character appears, the first character shall prevail. If no substring is found, -1 is returned.

  JS is able to distinguish 360 from Google by various browsers | through the kernel (pro test available)


function getBrowserInfo(){
    var ua = navigator.userAgent.toLocaleLowerCase();
    var browserType=null;
    if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
      browserType = "IE";
      browserVersion = ua.match(/msie ([d.]+)/) != null ? ua.match(/msie ([d.]+)/)[1] : ua.match(/rv:([d.]+)/)[1];
    } else if (ua.match(/firefox/) != null) {
      browserType = " firefox ";
    }else if (ua.match(/ubrowser/) != null) {
      browserType = "UC";
    }else if (ua.match(/opera/) != null) {
      browserType = " European friends ";
    } else if (ua.match(/bidubrowser/) != null) {
      browserType = " baidu ";
    }else if (ua.match(/metasr/) != null) {
      browserType = " sogou ";
    }else if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
      browserType = "QQ";
    }else if (ua.match(/maxthon/) != null) {
      browserType = " To navigate ";
    }else if (ua.match(/chrome/) != null) {
      var is360 = _mime("type", "application/vnd.chromium.remoting-viewer");
      function _mime(option, value) {
        var mimeTypes = navigator.mimeTypes;
        for (var mt in mimeTypes) {
          if (mimeTypes[mt][option] == value) {
            return true;
          }
        }
        return false;
      }
      if(is360){
        browserType = '360';
      }else{
        $('html').css("zoom",".80");
      }
    }else if (ua.match(/safari/) != null) {
      browserType = "Safari";
    }
}

Only existed in the original Chrome a MimeType "application/VND. Chromium. The remoting - viewer", which can determine the browser is packer Chrome or native Chrome.
For example, only browsers with the IE kernel have ActiveXObject objects. From this, you can determine whether you are an Internet explorer browser

To determine the browser type, we need to follow the following principles:


Related articles: