Home  >  Article  >  Web Front-end  >  js method to determine browser version and browser kernel_javascript skills

js method to determine browser version and browser kernel_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:18:561905browse

The example in this article describes how js determines the browser version and browser kernel. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:

if (!browser.ie && !browser.mac) {
var UA = navigator.userAgent.toLowerCase().toString();
//Determine whether 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('worldchrome') > -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 switch to a browser with IE core');
          }                                                                                                    }  
else {
//Determine 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 = {
//Detect whether the current browser is IE
          ie: /(msies|trident.*rv:)([w.] )/.test(agent),

//Detect whether the current browser is Opera
Opera: (!!opera && opera.version),
//Detect whether the current browser is a webkit-based browser

webkit: (agent.indexOf(' applewebkit/') > -1),

//Detect whether the current browser is running on the mac platform

mac: (agent.indexOf('macintosh') > -1),

//Detect whether the current browser is in "weird mode"

quirks: (document.compatMode == 'BackCompat')
};

//Detect whether the current browser kernel is 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(/(?:msies([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 if (v2 && v2[1]) {
Version = v2[1] * 1;
          } else {                                                 version = 0;
         } 

//Detect whether the browser mode is IE11 compatibility mode
browser.ie11Compat = document.documentMode == 11;

//Detect whether the browser mode is IE9 compatibility mode
browser.ie9Compat = document.documentMode == 9;

//Detect whether the browser mode is IE10 compatible mode
browser.ie10Compat = document.documentMode == 10;

//Detect whether the browser is IE8 browser
browser.ie8 = !!document.documentMode;

//Detect whether the browser mode is IE8 compatible mode
browser.ie8Compat = document.documentMode == 8;

//Detect whether the browser mode is IE7 compatibility mode
browser.ie7Compat = ((version == 7 && !document.documentMode) || document.documentMode == 7);

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

//Detect whether the current browser is Chrome, if so, return Chrome's large version number

If (/chrome/(d .d)/i.test(agent)) {
        browser.chrome = RegExp['x241'];
}  

//Detect whether the current browser is Safari, if so, return 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]);

//Detect the current browser version number

browser.version = version;

return browser;

}();

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn