"JavaScript" browser user agent detection script implementation

Source: Internet
Author: User

The following is a complete user agent string detection script, including detection of the rendering engine, platform, Windows operating system, mobile device, and game system.

var client = function () {//render engine var engine = {ie:0,gecko:0,webkit:0,khtml:0,opera:0,//full version number ver:null};//browser Var brow  Ser = {//main browser ie:0, firefox:0, safari:0, konq:0, opera:0, chrome:0,//specific version number ver: null};//platform, device, and operating system var system = {win:false,mac:false,x11:false,//mobile device Iphone:false, Ipod:false, ipad: False, Ios:false, Android:false, Nokian:false, winmobile:false,//game system Wii:false,ps:false        };//detection rendering engine and browser var ua = navigator.useragent;        if (window.opera) {engine.ver = Browser.ver = Window.opera.version ();    Engine.opera = Browser.opera = parsefloat (engine.ver);        } else if (/applewebkit\/(\s+)/.test (UA)) {Engine.ver = regexp["$"]; Engine.webkit = parsefloat (engine.ver);//Determine if Chroeme or Safari if (/chrome\/(\s+)/.test (UA)) {browser.ver = RegEx            P["$"];        Browser.chrome = parsefloat (browser.ver); } else if (/version\/(\s+)/.test (UA)) {            Browser.ver = regexp["$"];        Browser.safari = parsefloat (browser.ver);            } else {//approximately determines the version number var safariversion = 1;            if (Engine.webkit <) {safariversion = 1;            } else if (Engine.webkit < 312) {safariversion = 1.2;            } else if (Engine.webkit < 412) {safariversion = 1.3;            } else {safariversion = 2;                } Browser.safari = Browser.ver = safariversion; }} else if (/khtml\/(\s+)/.test (UA) | |/konqueror\/([^;]        +)/.test (UA)) {engine.ver = Browser.ver = regexp["$"];    engine.khtml = Browser.konq = parsefloat (engine.ver);        } else if (/RV: ([^\)]+) \) gecko\/\d{8}/.test (UA)) {Engine.ver = regexp["$"];       Engine.gecko = parsefloat (engine.ver);            Determine if 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);    }//detection Browser browser.ie = engine.ie; Browser.opera = engine.opera;//detection platform var p = Navigator.platform;system.win = P.indexof ("win") = = 0;system.mac = P.indexOf (" Mac ") = = 0;system.x11 = (p = =" x11 ") | | (P.indexof ("Linux") = = 0);//Detect Windows operating system if (System.win) {if (/win (?:d ows)? ( [^do] {2}) \s? (\d+\.\d+)/.test (UA) {if (regexp["] = =" NT ") {switch (regexp[" $ "]) {case" 5.0 ": System.win =" $ "; Break;case" 5.1 " : System.win = "XP"; break;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 device System.iphone = Ua.indexof ("iphone") > -1;system.ipod = Ua.indexof ("ipod") > -1;system.ipad = Ua.indexof ("IP Ad ") >-1;system.nokian = Ua.indexof (" Nokian ") > -1;//Windows Mobileif (sYstem.win = = "CE") {system.winmobile = System.win;} else if (System.win = = "Ph") {if (/windows phone OS (\d+.\d+)/.test (UA)) {System.win = "phone"; system.winmobile = parsefloat (regexp["$"]);}} Detect iOS version if (System.mac && ua.indexof ("Mobile") >-1) {if (/cpu (?:P hone)? OS (\d+_\d+)/.test (UA)) {System.ios = parsefloat (Regexp.$1.replace ("_", "."));} Else{system.ios = 2;//can not be really detected, so can only guess}}//detect Android version if (/android (\d+\.\d+)/.test (UA)) {system.android = parsefloat ( REGEXP.$1);} Game system system.wii = Ua.indexof ("Wii") > -1;system.ps =/playstation/i.test (UA);//Returns these objects return {Engine:eng  INE, Browser:browser, system:system};   Console.log (engine);   Console.log (browser); Console.log (System);} ();

  

"JavaScript" browser user agent detection script implementation

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.