JavaScript determines browser type and version

Source: Internet
Author: User

Ie

Only IE supports the creation of ActiveX controls, so she has something that other browsers do not have, which is the ActiveXObject function. As long as the Window object exists ActiveXObject function, it can be clearly determined that the current browser is ie. The typical useragent for each version of IE are as follows:

mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)
mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)
mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
mozilla/4.0 (compatible; MSIE 5.0; Windows NT)

Where the version number is the number after MSIE.

Firefox

The DOM element in Firefox has a getboxobjectfor function to get the location and size of the DOM element (ie corresponds to the getboundingclientrect function). This is unique to Firefox, you can tell it is the current browser is Firefox. Several versions of useragent in Firefox are as follows:

mozilla/5.0 (Windows; U Windows NT 5.2) gecko/2008070208 firefox/3.0.1
mozilla/5.0 (Windows; U Windows NT 5.1) gecko/20070309 firefox/2.0.0.3
mozilla/5.0 (Windows; U Windows NT 5.1) gecko/20070803 firefox/1.5.0.12
Where the version number is the number after Firefox.

Opera

Opera offers a special browser logo, which is the Window.opera property. Opera's typical useragent are as follows:

opera/9.27 (Windows NT 5.2; U ZH-CN)
opera/8.0 (Macintosh; PPC Mac OS X; U En
mozilla/5.0 (Macintosh; PPC Mac OS X; U EN) Opera 8.0

Where the version number is the number near opera.

Safari

The Safari browser has a opendatabase function that no other browser can do to judge Safari's logo. Safari's typical useragent are as follows:

mozilla/5.0 (Windows; U Windows NT 5.2) applewebkit/525.13 (khtml, like Gecko) version/3.1 safari/525.13
mozilla/5.0 (IPhone; U CPU like Mac OS X) applewebkit/420.1 (khtml, like Gecko) version/3.0 mobile/4a93 safari/419.3

The version number is the number that follows versions.

Chrome

Chrome has a messageevent function, but Firefox does too. However, the good news is that Chrome does not have a Firefox getboxobjectfor function, which can be used to accurately determine the Chrome browser. Currently, Chrome's useragent is:

mozilla/5.0 (Windows; U Windows NT 5.2) applewebkit/525.13 (khtml, like Gecko) chrome/0.2.149.27 safari/525.13

Where the version number is in Chrome only after the number.

Interestingly, Chrome's useragent also contains the features of Safari, which is perhaps the basis for Chrome to run all Apple browser apps.

As long as we understand the above information, we can base these features to determine the browser type and its version. We will store the results of the judgment in the Sys namespace, which will be the basic flag information of the front-end framework for future programs to read. If a browser is identified, the Sys namespace will have a property of the browser name, and its value is the version number of the browser. For example, if IE 7.0 is determined, the value of sys.ie is 7.0, and if Firefox 3.0 is determined, the value of Sys.firefox is 3.0. The following is the code that determines the browser:

<script type= "Text/javascript" >

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;

The following tests

if (sys.ie) document.write (' ie: ' + sys.ie);

if (Sys.firefox) document.write (' Firefox: ' + Sys.firefox);

if (sys.chrome) document.write (' Chrome: ' + sys.chrome);

if (Sys.opera) document.write (' Opera: ' + Sys.opera);

if (Sys.safari) document.write (' Safari: ' + Sys.safari);

</script>

We put the judgment on IE first, because the most users of IE, followed by the determination of Firefox. According to the number of users in the order to determine the browser type, can improve judgment efficiency, less do not work hard. The reason we put Chrome in third judgment is because we predict that Chrome will soon become the third-place browser for market share. In the analysis of the browser version, a regular expression is used to extract the version information.

If your JavaScript is playing very high, you can also write the previous judgment code like this:

<script type= "Text/javascript" >

var Sys = {};

var ua = Navigator.userAgent.toLowerCase ();

Window. ActiveXObject? sys.ie = Ua.match (/msie ([\d.] +)/) [1]:

Document.getboxobjectfor? Sys.firefox = Ua.match (/firefox\/([\d.] +)/) [1]:

Window. Messageevent &&!document.getboxobjectfor? Sys.chrome = Ua.match (/chrome\/([\d.] +)/) [1]:

Window.opera? Sys.opera = Ua.match (/opera. ( [\d.] +)/) [1]:

Window.opendatabase? Sys.safari = Ua.match (/version\/([\d.] +)/) [1]: 0;

The following tests

if (sys.ie) document.write (' ie: ' +sys.ie);

if (Sys.firefox) document.write (' Firefox: ' +sys.firefox);

if (sys.chrome) document.write (' Chrome: ' +sys.chrome);

if (Sys.opera) document.write (' Opera: ' +sys.opera);

if (Sys.safari) document.write (' Safari: ' +sys.safari);

</script>

This will make the JavaScript code more streamlined. Of course, the readability is slightly worse, it depends on whether you pay attention to efficiency or maintainability.

Using different features to determine the browser's approach, although it is faster to analyze useragent than with regular expressions, these features may change depending on the browser version. For example, a browser-unique feature has been successful on the market, and other browsers may then join the feature, so that the unique features of the browser disappear, causing our judgment to fail. Therefore, it is relatively safe to determine the browser type by parsing the features in the useragent. Besides, judging the version information also needs to parse the browser's useragent.

By analyzing the useragent information of various browsers, it is not difficult to get a regular expression that distinguishes between various browsers and their versions. Moreover, the judgment of the browser type and version of the judgment can be completely integrated. So, we can write the following code:

<script type= "Text/javascript" >

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;

The following tests

if (sys.ie) document.write (' ie: ' + sys.ie);

if (Sys.firefox) document.write (' Firefox: ' + Sys.firefox);

if (sys.chrome) document.write (' Chrome: ' + sys.chrome);

if (Sys.opera) document.write (' Opera: ' + Sys.opera);

if (Sys.safari) document.write (' Safari: ' + Sys.safari);

</script>

Among them, the adoption of the "...?" A judgment expression such as "..." to streamline the code. The judging condition is an assignment statement, which does not only match the regular expression but also the result copy, and then judge the condition directly. The subsequent version information can only be extracted from the previous matching results, which is very efficient code.

The above code is designed to create a front-end framework for pre-research, and test pass on the top five browsers. In the future, it is very graceful to judge whether a browser needs to be expressed in the form of if (sys.ie) or if (Sys.firefox), or if the browser version only needs to use if (sys.ie = = ' 8.0 ') or if (Sys.firefox = = ' 3.0 ').

The front-end framework project has started, everything depends on the process and results ...

Original: Li Shi (Leadzen) Ali software 2008-9-6 Hangzhou

Original: http://www.cnblogs.com/leadzen/archive/2008/09/06/1285764.html

Other methods: (ie11 Invalid )

<script type= "Text/javascript";
alert (window.navigator.userAgent);
var request = false;
Var btype =getinternet ();
Function getinternet ()
{
if (navigator.userAgent.indexOf ("MSIE") >0) {
return "MSIE";//ie browser
}
if (Isfirefox=navigator.useragent.indexof ("Firefox") >0) {
return "Firefox";//firefox browser
}
if ( Issafari=navigator.useragent.indexof ("Safari") >0) {
return "Safari";//safan browser
}
if (iscamino= Navigator.userAgent.indexOf ("Camino") >0) {
return "Camino";//camino browser
}
if (ismozilla= Navigator.userAgent.indexOf ("gecko/") >0) {
return "Gecko";//gecko browser
}
}
try {
Request = new XMLHT Tprequest ();
} catch (Trymicrosoft) {
try {
request = new ActiveXObject ("Msxml2.xmlhttp");//support for Microsoft
} catch (Other Microsoft) {
try {
request = new ActiveXObject ("Microsoft.XMLHTTP");//support for non-Microsoft
} catch (failed) {
Requ EST = false;
}
}
}

if (!request)
Alert ("error!! The browser is not safe, please select a higher version of the Tour! ");


</script>

Related Article

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.