Use userAgent to determine whether the string returned by userAgent contains "Chrome". How to detect it is done through the indexOf method.
<script type="text/javascript"> var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1; alert(isChrome); if (isChrome) { alert("isChrome Ch rome browser") ; } else { alert("not Chrome"); }</script>
About indexOf method:
The indexOf method returns an integer value indicating the start position of the substring inside the String object. That is, the position of the characters contained in indexOf() brackets in the string is returned at the number of digits, and counting starts from 0. If there are duplicate characters, the first character shall prevail. If no substring is found, return -1.
JS judges various browsers through the kernel | distinguishes between 360 and Google (professional test available)
function getBrowserInfo(){ var ua = navigator.userAgent.toLocaleLowerCase(); var browserType=null; if (ua.match(/msie/) != null || ua.match(/t rident/) != 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 = "Openg"; } 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 = "Running"; }else if (ua.match (/chrome/) != null) { var is360 = _mime("type", "application/vnd.chromium.remoting-viewer"); function _mime(option, value) { var mimeTypes = navi gator.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 a MimeType "application/vnd.chromium.remoting-viewer" exists in native Chrome, which can be judged whether the browser is shelled with Chrome or native Chrome.
For example, only the browser of the IE kernel has an ActiveXObject object. From this we can determine whether it is an IE browser
To determine the browser type, we need to follow the following principles:
1. Adopt the hit feature principle, and we will only adopt this feature if and only if it completely matches the characteristics that distinguish the browser. For example, it is unreliable to simply detect whether an IE browser is simply through MSIE in UA. By judging whether there is a MimeType "application/vnd.chromium.remoting-viewer" to assert that native Chrome is feasible at this stage, but it does not guarantee permanent effectiveness.
2. There is generally no UserAgent keyword conflict for mainstream browsers, but it is not certain for many shell browsers. I proposed a digital browser again. UserAgent is exactly the same as IE, but the rendering mode and so on are very different, which is very different from the standard IE behavior. When judging the browser by userAgent, the browser feature word is preferred. A match is basically determined to be the browser, but not matching does not mean that it is not the browser. Please know everything.
3. Priority is given to using browser features to distinguish browsers because this is more accurate. Secondly, userAgent is used to assist judgment to achieve the highest degree of matching.
4. Priority is given to detecting third-party shell browsers. There is no very good solution at present. You can only enumerate most browsers that exist and can be judged on the world. Other browsers that do not match any rules. For compatibility, please set them. The rules fall into one of the four major browsers.
5. To judge the browser version, it is only for optimization for specific browsers, and it requires specific business scenarios to do so. Or it is only judged when a browser has compatibility issues and urgently adds patch code for the browser. A more scientific and safe way is to use standard JS functions and APIs, and the page elements and style designs follow the W3C standard. There may be disputed compatibility issues. Use third-party frameworks such as jQuery whenever possible. This is the fundamental solution to the compatibility problem.
The following is the code that recently compiled to determine the browser type