Js A Simple Method to Obtain and Judge Browser Version Information

  • 2021-07-09 06:56:40
  • OfStack

The Navigator object contains information about the browser:

• appCodeName--String representation of browser code name

• appName--String representation of official browser name

• ES 10 EN--String representation of browser version information

• cookieEnabled-Returns true if cookie is enabled, otherwise returns false

• javaEnabled-Returns true if java is enabled, otherwise returns false

• ES 25EN--String representation of the computer platform where the browser is located

• ES 28EN--Array of plug-ins installed in the browser

• taintEnabled-Returns true if data stain is enabled, otherwise returns false

• ES 36EN--String representation of user agent headers

The most important thing in navigator is the userAgent attribute, which returns a string containing information such as browser version;

cookieEnabled is also very important, using it to determine whether the user's browser has cookie turned on.

There are two ways to judge browser type 1 by javaScript, one is to distinguish according to the unique attributes of various browsers, and the other is to judge by analyzing the userAgent attributes of browsers (the version can only be obtained by analyzing userAgent);

Compatibility issues can only be handled after both browser type and browser version have been determined.

1. Judge the browser type and version by the characteristics in userAgent (common and safe practice)


function getBrowserInfo() {
    var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s; (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
    (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
    (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
    (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
    (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;

    if(Sys.ie) {
          return 'IE: ' + Sys.ie;
    }
    if(Sys.firefox) {
          return 'Firefox: ' + Sys.firefox;
    }
    if(Sys.chrome) {
          return 'Chrome: ' + Sys.chrome;
    }
    if(Sys.opera) {
          return 'Opera: ' + Sys.opera;
    }
    if(Sys.safari) {
          return 'Safari: ' + Sys.safari;
    }
} 
var browser = getBrowserInfo() ;
var verinfo = (browser+"").replace(/[^0-9.]/ig, "");   //  Version number 

Note: 1. Chrome and Safari are found in userAgent attribute values of some browsers, because userAgent of Chrome also contains the characteristics of Safari, so this may be the basic reason why Chrome can run Safari browser applications.

2. Distinguish browsers by their unique features (note that these features may change with the browser version, or other browsers may add this feature along with them, resulting in failure of judgment)

IE: Only IE supports creating ActiveX controls, so ActiveXObject functions are not available in other browsers. Just judge that the IE function exists in the window object, and then clearly judge that the current browser is IE.

Firefox: Each DOM element in FF has an getBoxObjectFor function to obtain the position and size of the DOM element. This is unique to Firefox, and it can be judged that the current browser is Firefox. (IE corresponds to the getBoundingClientRect function)

Opera: Opera provides a special browser flag-window. opera attribute.

Safari: The openDatabase function is not available in other browsers and can be used as a sign to judge Safari.

Chrome: Both FF1 and MessageEvent function, but Chrome does not have getBoxObjectFor function of FF. According to these two conditions, Chrome browser can be judged.


var Sys = {};
var ua = navigator.userAgent.toLowerCase();
if(window.ActiveXObject) {
    Sys.ie = ua.match(/msie ([\d.]+)/)[1]
}else if(document.getBoxObjectFor) {
    Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
}else if(window.MessageEvent && !document.getBoxObjectFor) {
    Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1]
}else if(window.opera) {
    Sys.opera = ua.match(/opera.([\d.]+)/)[1]
}else if(window.openDatabase) {
    Sys.safari = ua.match(/version\/([\d.]+)/)[1];
}

The level is limited, and mistakes in the text are inevitable. Welcome to criticize, correct and suggest comments. The article will be revised and improved from time to time. Thank you!


Related articles: