Judge the type of browser you use perfect code

Source: Internet
Author: User

The code is as follows:

The code is as follows Copy Code

<script language= "JavaScript" type= "Text/javascript" >
if ((Navigator.userAgent.indexOf (' msie ') >= 0) && (navigator.userAgent.indexOf (' Opera ') < 0)) {alert (' You are using IE ')}else
if (navigator.userAgent.indexOf (' Firefox ') >= 0) {alert (' You are using Firefox ')}else
if (Navigator.userAgent.indexOf (' opera ') >= 0) {alert (' You are using Opera ')}else
{alert (' You are browsing the Web with other browsers! ')}
</script>

This code is judged to include not only computer browsers, but also Android, iOS mobile phones and tablets, gaming systems

  code is as follows copy code

var client = function () {
   //Render engine
    var engine ={
   & nbsp;    ie:0,
        gecko:0,
         webkit:0,
        khtml:0,
    & nbsp;   opera:0,
      
         Ver:null
   };
   /Browser
    var browser = {
        ie: 0,
        firefox:0,
        Safari  : 0,
        konq:0,
        opera:0,
        chrome:0,

Ver:null
};

var system ={
Win:false,
Max:false,
X11:false,

Mobile devices
Iphone:false,
Ipod:false,
Ipad:false,
Ios:false,
Android:false,
Nokian:false,
Winmoble:false,

Game system
Wii:false,
Ps:false
};

var ua =navigator.useragent;

Opera 9 After the emergence of two kinds of string proxy way one way is to identify themselves to another browser another way is to mark themselves as Firefox or IE
In the latter case, the user agent string is actually the same as that of the other browsers--neither opera's words nor opera's version information
So the browser starts with opera.
if (Winddow.opera) {
Engine.ver = Browser.ver = Window.opera.version ();
Egine.opera = Browser.opera = parsefloat (engine.ver);
The second detection WebKit is due to WebKit's user agent Gecko "and" HTMKL "strings so if you detect them first, they all have the wrong conclusion.
}else if (/applewebkit/(s+)/.test (UA)) {
Engin.ver = regexp["$"];
Engine.webkit = parsefloat (engine.ver);

Sure it's Chrome or safari?
if (/chrome/(s+)/.test (UA) {
Browser.ver = regexp["$"];
Browser.chrome = parsefloat (brower.ver);
}else if (/version/(+s)/.test (UA)) {//safari 3.0 adds the Version property
Browser.ver = regexp["$"];
Browser.safari = parsefloat (brower.ver);
}else{
var safariversion = 1;
if (engine.webkit<100) {
Safariversion = 1;
}else if (engine.ver<312) {
Safariversion = 1.2;
}else if (engine.ver<412) {
Safariversion = 1.3;
}else{
Safariversion = 2;
}

Browser.safari = Browser.ver = safariversion;
}
}else if (/khtml/(s+)/.test (UA) | | /konqueror/([^;] +)/.test (UA)) {//linux browser
Engine.ver = Browser.ver = regexp["$";
engine.khtml = parsefloat (engine.ver);
}else if (/rv: ([^)]+) Gocko/d{8}/.test (UA)) {
Engine.ver = regexp["$"];
Engine.gecko = parsefloat (engine.ver);

Sure it's not Firefox.
if (/firefox/(s+)/.test (UA) {
Browser.ver = regexp["$"];
Browser.firefox = parsefloat (browser.ver);
}
}else if (/msie) ([^;] +)/.test (UA)) {
Engine.ver = Browser.ver = regexp["$";
engine.ie = browser.ie = parsefloat (engine.ver);
}

detecting browsers
Browser.ie =engine.ie;
Browser.opera = Engine.opera;

var p = navigator.platform;
System.win = P.indexof ("Win") ==0;
System.mac = P.indexof ("Mac") ==0;
system.x11 = (p== "x11") | | P.indexof ("Linux") ==0;


if (System.win) {
if (/win (?:d ow)? ( [^do] {2}s? (d+.d+)/.test (UA)) {
if (regexp["$"]== "NT") {
Switch (regexp["$"]) {
Case "5.0":
System.win = "2000";
Break
Case "5.1":
System.win = "XP";
Case "6.0":
System.win = "Vista";
Break
Case "6.1":
System.win = "7";
Break
Default:
System.win = "NT";
Break
}
}else if (regexp["$"]== "9x") {
System.win = "ME";
}else{
System.win = regexp["$"];
}
}
}


Mobile devices
System.iphone = Ua.indexof ("iphone") >-1;
System.ipod = Ua.indexof ("ipod") >-1;
System.ipad = Ua.indexof ("ipad") >-1;
System.nokian = Ua.indexof ("Nokian") >-1;

   //win Mobile
    if (System.win = = "CE") {
     & nbsp;  system.winmobile = System.win;
   }else if (System.win = "Ph") {
        if/windows Phone OS (d+_d+)/.test (UA) {
            System.win = "Phone";
            system.winmobile parsefloat (regexp["$");
       }
   }

detecting iOS versions
//
if (System.mac && ua.indexof ("Mobile") >-1) {
if (/cpu: IPhone)? OS (d+_d+)/.test (UA) {
System.ios = parsefloat (Rexexp.$1.replace ("_", "."));
}else {
System.ios = 2;//can't really check out, so guess out a result
}
}


Check an Android version
//
if (/android (d+/_d+)/.test (UA)) {
System.andriod = parsefloat (regexp.$1);
}

Game system

System.wii = Us.indexof ("Wii") >-1;
system.ps =/playstation/i.test (UA);

Return object
return {
Engine:engine,
Browser:browser,
System:system
}

}
  


The following are the features of various browsers and their useragent.

Ie

Only IE supports the creation of ActiveX controls, so she has something that other browsers do not have, that is, the ActiveXObject function. As long as you judge the Window object exists ActiveXObject function, you can clearly determine the current browser is ie. The typical useragent of IE versions 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 the MSIE.

Firefox

The DOM elements in Firefox have a getboxobjectfor function to get the position and size of the DOM element (ie corresponds to the getboundingclientrect function). This is unique to Firefox, judge it can know is the current browser is Firefox. Firefox several versions of the useragent are roughly 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 flag, which is the Window.opera property. Opera's typical useragent is 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


Among them, the version number is close to opera's number.


Safari


Safari browser has a opendatabase function that is not available in other browsers and can be used as a marker for safari. Safari's typical useragent is 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 after version.

Chrome

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


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

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.