A simple way to get and judge browser version information _javascript skills

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--string representation of browser version information

cookieenabled-returns False if the Enable cookie returns True

javaenabled-Returns False if Java is enabled to return True

platform--a string representation of the computer platform on which the browser resides

Plugins--an array of plug-ins installed in the browser

taintenabled-returns False if a data stain is enabled to return True

useragent--string representation of user agent headers

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, using it to determine whether a user's browser has cookies open.

JavaScript Judge browser type generally there are two ways, one is based on the unique properties of various browsers to distinguish, the other is through the analysis of browser useragent properties to judge (version can only be obtained through the analysis useragent);

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

1, through the characteristics of useragent to determine the browser type and version (commonly used, 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 values for Chrome, Safari, because Chrome useragent also includes safari features, so this may be the reason why Chrome can run Safari browser apps.

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

ie: only IE supports the creation of ActiveX controls, so the ActiveXObject function is not available in other browsers. Simply judge that the window object has a ActiveXObject function, and it is clear that the current browser is ie.

Dom elements in Firefox:ff have a getboxobjectfor function to get the position and size of the DOM element. This is unique to Firefox, judge it can tell 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 safari.

Chrome: A messageevent function like FF, but Chrome does not have FF's getboxobjectfor function, which can be used to determine the 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 article is unavoidable, welcome to criticize the suggestion comment. The article will revise and perfect treatise regularly. Thank you!

The above JS to obtain and judge the browser version of the information is a simple way to share the whole of the content of everyone, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.