This article describes how js judges the browser version and browser kernel. Share it for your reference. The specific implementation method is as follows:
JS determines whether the mobile terminal and browser kernel
var browser = { versions: function() { var u = navigator.userAgent; return {trident: u.indexOf('Trident') > -1, //IE kernel presto: u.indexOf('Presto') > -1, //opera kernel webKit: u.indexOf('AppleWebKit') > -1, //Apple and Google kernel gecko: u.indexOf('Firefox') > -1, //Firefox kernel Geckomobile: !!u.match(/AppleWebKit.*Mobile.*/), //Is it a mobile terminal ios: !!u.match(//(i[^;]+;( U;)? CPU.+Mac OS X/), //iosandroid: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //androidiPhone: u.indexOf('iPhone') > -1 , //iPhoneiPad: u.indexOf('iPad') > -1, //iPadwebApp: u.indexOf('Safari') > -1 //Safari};}()}if (browser.versions.mobile || browser.versions.ios || browser.versions.android || browser.versions.iPhone || browser.versions.iPad) {alert('mobile');}Code Two
document.write(navigator.userAgent+'<br><br>')document.write(browser.versions.trident+'<span>//ie</span> <br>')document.write(browser.versions.presto+'<span>//opera</span> <br>')document.write(browser.versions.webKit+'<span>//Apple, Google Kernel</span> <br>')document.write(browser.versions.gecko+'<span>//Firefox Kernel</span> <br>')document.write(browser.versions.mobile+'<span>//Is it a mobile terminal</span> <br>')document.write(browser.versions.ios+'<span>//ios</span> <br>')document.write(browser.versions.android+'<span>//android</span> <br>')document.write(browser.versions.iPhone+'<span>//iPhone</span> <br>')document.write(browser.versions.webApp+'<span>//Safari</span> <br>')document.write(browser.versions.webApp+'<span>//Safari</span> <br>')document.write(browser.versions.webApp+'
if (!browser.ie && !browser.mac) { var UA = navigator.userAgent.toLowerCase().toString(); //Judge if it is a non-IE version under the IE kernel if ((UA.indexOf('360ee') > -1) || (UA.indexOf('360se') > -1) || (UA.indexOf('se') > -1) || (UA.indexOf('aoyou') > -1) || (UA.indexOf('theworld') > -1) || (UA.indexOf('world') > -1) || (UA.indexOf('world') > -1) || (UA.indexOf('world') > -1) || (UA.indexOf('world') > -1) || (UA.indexOf('world') > -1) || (UA.indexOf('greenbrowser') > -1) || (UA.indexOf('baidu') > -1) || (UA.indexOf('qqbrowser') > -1)) { //If yes, switch to compatibility mode window.open("publicPage/point-se.aspx"); } else { //If not, it is recommended to change the browser alert('It is recommended to change to the browser of IE kernel'); } } else { //Judge the version model of IE if ( (browser.version == 10 && browser.ie10Compat) || (browser.version == 11 && browser.ie11Compat)) { window.open("publicPage/point.aspx"); } /* * @desc Determine the browser version and browser kernel* @author wangyanling * @date July 4, 2014*/ var browser = function () { var agent = navigator.userAgent.toLowerCase(), opera = window.opera, browser = { //Detection whether the current browser is IE ie: /(msie/s|trident.*rv:)([/w.]+)/.test(agent), //Detection whether the current browser is Opera opera: (!!opera && opera.version), //Detection whether the current browser is the webkit kernel webkit: (agent.indexOf('applewebkit/') > -1), //Detection whether the current browser is running on the mac platform mac: (agent.indexOf('macintosh') > -1), //Detection whether the current browser is in "weird mode" quirks: (document.compatMode == 'BackCompat') }; //Detection whether the current browser kernel is the gecko kernel browser.gecko = (navigator.product == 'Gecko' && !browser.webkit && !browser.opera && !browser.ie); var version = 0; // Internet Explorer 6.0+ if (browser.ie) { var v1 = agent.match(/(?:msie/s([/w.]+))/); var v2 = agent.match(/(?:trident.*rv:([/w.]+))/); if (v1 && v2 && v1[1] && v2[1]) { version = Math.max(v1[1] * 1, v2[1] * 1); } else if (v1 && v1[1]) { version = v1[1] * 1; } else { version = 0; } //Detection whether the browser mode is IE11 compatible mode browser.ie11Compat = document.documentMode == 11; //Detection whether the browser mode is IE9 compatible mode browser.ie9Compat = document.documentMode == 9; //Detection whether the browser mode is IE10 compatible mode browser.ie10Compat = document.documentMode == 10; //Detection whether the browser is IE8 browser browser.ie8 = !!document.documentMode; //Detection whether the browser mode is IE8 compatible mode browser.ie8Compat = document.documentMode == 8; //Detection whether the browser mode is IE7 compatible mode browser.ie7Compat = ((version == 7 && !document.documentMode) || document.documentMode == 7); //Detection whether the browser mode is IE6 mode or weird mode browser.ie6Compat = (version < 7 || browser.quirks); browser.ie9above = version > 8; browser.ie9below = version < 9; } // Gecko. if (browser.gecko) { var geckoRelease = agent.match(/rv:([/d/.]+)/); if (geckoRelease) { geckoRelease = geckoRelease[1].split('.'); version = geckoRelease[0] * 10000 + (geckoRelease[1] || 0) * 100 + (geckoRelease[2] || 0) * 1; } } //Detection whether the current browser is Chrome, if yes, it returns the large version number of Chrome if (/chrome//(/d+/./d)/i.test(agent)) { browser.chrome = +RegExp['/x241']; } //Detection whether the current browser is Safari, if yes, it returns the large version number of Safari if (/(/d+/./d)?(?:/./d)?/s+safari//?(/d+/./d+)?/i.test(agent) && !/chrome/i.test(agent)) { browser.safari = +(RegExp['/x241'] || RegExp['/x242']); } // Opera 9.50+ if (browser.opera) version = parseFloat(opera.version()); // WebKit 522+ (Safari 3+) if (browser.webkit) version = parseFloat(agent.match(// applewebkit//(/d+)/)[1]); //Check the current browser version number browser.version = version; return browser; }();I hope this article will be helpful to everyone's JavaScript programming.