Js-Get and Judge browser version information

Source: Internet
Author: User

The Navigator object contains information about the browser:

    • appCodeName--A string representation of the browser code name
    • AppName--A string representation of the official browser name
    • AppVersion--A string representation of browser version information
    • Cookieenabled--Returns True if cookie is enabled, FALSE otherwise
    • Javaenabled--Returns True if Java is enabled, otherwise false
    • Platform--a string representation of the computer platform on which the browser resides
    • Plugins--an array of plugins installed in the browser
    • Taintenabled--Returns True if the data stain is enabled, otherwise false
    • UserAgent--A string representation of the user agent header

The most important thing in Navigator is the UserAgent property, which returns a string containing information such as the browser version;

Cookieenabled is also important, and it can be used to determine whether a cookie is turned on by the user's browser.

JavaScript can judge the browser type generally have two methods, one is based on a variety of browser unique properties to distinguish, and the other is by analyzing the browser's UserAgent property to judge ( version only through the analysis of useragent );

Compatibility issues can only be handled after the browser type and browser version have been judged .

1, through the characteristics of the useragent to determine the browser type and version ( Common, insurance practices)

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: Some browsers have useragent attribute values in Chrome,Safari, because Chrome's useragent also includes Safari features, so this could be The reason that Chrome can run the base of the Safari browser app .

2, through the unique features of the browser to distinguish the browser (note: These features may change with the browser version, or other browsers may also join the feature, resulting in a failure of judgment)

ie: only IE supports the creation of ActiveX controls, so the ActiveXObject function is not available in other browsers. Simply determine the existence of the Window object ActiveXObject function, it is clear that the current browser is ie.

Dom elements in Firefox:ff have a getboxobjectfor function to get the location and size of the DOM element. This is unique to Firefox, judging it can tell that the current browser is Firefox. (ie corresponds to the getboundingclientrect function)

Opera:opera provides a dedicated browser flag-the Window.opera attribute.

The Safari:opendatabase function is not available in other browsers and can be used as a marker for judging safari.

Chrome: Like FF is a messageevent function, but Chrome does not have FF's getboxobjectfor function, according to these two conditions can judge Chrome browser.

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, the error in the text is unavoidable, welcome to criticize the suggestion comment. The article will revise and perfect the treatise on an irregular basis. Thank you!

Js-Get and Judge browser version information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.