The Navigator object contains information about the browser:
•appCodeName -- string representation of browser code name
•appName -- string representation of the official browser name
•appVersion -- string representation of browser version information
•cookieEnabled -- Return true if cookie is enabled, otherwise return false
•javaEnabled -- Return true if java is enabled, otherwise return false
•platform -- The string representation of the computer platform where the browser is located
•plugins -- an array of plugins installed in the browser
•taintEnabled -- Return true if data stain is enabled, otherwise return false
•userAgent -- string representation of user agent header
The most important thing in the navigator is the userAgent attribute, which returns a string containing information such as browser version;
CookieEnabled is also important. Use it to determine whether the user's browser is enabled.
There are generally two ways to judge browser types in JavaScript. One is to distinguish them based on the unique attributes of various browsers, and the other is to judge by analyzing the browser's userAgent attribute (the version can only be obtained by analyzing userAgent);
Compatibility issues can only be handled after both the browser type and the browser version are determined.
1. Use the characteristics in userAgent to determine the browser type and version (commonly used, insurance practices)
function getBrowserInfo() { var Sys = {}; var ua = (); var s; (s = (/msie ([\d.]+)/)) ? = s[1] : (s = (/firefox\/([\d.]+)/)) ? = s[1] : (s = (/chrome\/([\d.]+)/)) ? = s[1] : (s = (/opera.([\d.]+)/)) ? = s[1] : (s = (/version\/([\d.]+).*safari/)) ? = s[1] : 0; if() { return 'IE: ' + ; } if() { return 'Firefox: ' + ; } if() { return 'Chrome: ' + ; } if() { return 'Opera: ' + ; } if() { return 'Safari: ' + ; } } var browser = getBrowserInfo() ; var verinfo = (browser+"").replace(/[^0-9.]/ig, ""); // Version number
Note: Chrome and Safari are available in the userAgent attribute value of some browsers because Chrome's userAgent also contains the characteristics of Safari, so this may be the basis for Chrome to run Safari browser applications.
2. Distinguish the browser through the unique features of each browser (note: these features may change with the browser version, or other browsers may also add this feature, resulting in failure of judgment)
IE: Only IE supports creating ActiveX controls, so the ActiveXObject function is not available to other browsers. Just judge that the window object exists as an ActiveXObject function, and you will clearly determine that the current browser is IE.
Firefox: The DOM elements in FF have a getBoxObjectFor function to get the position and size of the DOM element. This is unique to Firefox. You can tell that the current browser is Firefox. (The getBoundingClientRect function corresponding to IE)
Opera: Opera provides special browser logos -- attributes.
Safari: The openDatabase function is not available in other browsers and can be used as a sign to judge Safari.
Chrome: Like FF, there is a MessageEvent function, but Chrome does not have the getBoxObjectFor function of FF. Based on these two conditions, Chrome browser can be judged.
var Sys = {}; var ua = (); if() { = (/msie ([\d.]+)/)[1] }else if() { = (/firefox\/([\d.]+)/)[1] }else if( && !) { = (/chrome\/([\d.]+)/)[1] }else if() { = (/opera.([\d.]+)/)[1] }else if() { = (/version\/([\d.]+)/)[1]; }
The level is limited, and it is inevitable that the errors in the article are inappropriate. Criticism, corrections, suggestions and comments are welcome. The article will be revised and improved from time to time. Thanks!
The simple way to obtain and judge browser version information in the above article Js is all the content I share with you. I hope you can give you a reference and I hope you can support me more.