userAgent string detection in javascript

  1 var client = function () {
  2 
  3         //browser engine
  4         var engine = {
  5             ie: 0,
  6             gecko: 0,
  7             webkit: 0,
  8             khtml: 0,
  9             opera: 0,
 10             //full version
 11             ver: null
 12         };
 13 
 14         //browser
 15         var browser = {
 16 
 17             //browser
 18             ie: 0,
 19             firefox: 0,
 20             konq: 0,
 21             opera: 0,
 22             chrome: 0,
 23             safari: 0,
 24 
 25             //specific version
 26             ver: null
 27         };
 28 
 29         //platform,device and OS
 30         var system = {
 31             win: false,
 32             mac: false,
 33             xl1: false,
 34 
 35             //mobile device
 36             iphone: false,
 37             ipod: false,
 38             nokiaN: false,
 39             winMobile: false,
 40             macMobile: false,
 41 
 42             //game system
 43             wii: false,
 44             ps: false
 45         };
 46 
 47         //check browser and engine
 48         var ua = navigator.userAgent;
 49         if (window.opera) {
 50             engine.ver = browser.ver = window.opera.version();
 51             engine.opera = browser.opera = parseFloat(engine.ver);
 52         } else if (/AppleWebKit\/(\S+)/.test(ua)) {
 53             engine.ver = RegExp["$1"];
 54             engine.webkit = parseFloat(engine.ver);
 55 
 56             //check whether chrome or safari
 57             if (/Chrome\/(\S+)/.test(ua)) {
 58                 browser.ver = RegExp["$1"];
 59                 browser.chrome = parseFloat(browser.ver);
 60             } else if (/Version\/(\S+)/.test(ua)) {
 61                 browser.ver = RegExp["$1"];
 62                 browser.safari = parseFloat(browser.ver);
 63             } else {
 64                 //check version
 65                 var safariVersion = 1;
 66                 if (engine.webkit < 100) {
 67                     safariVersion = 1;
 68                 } else if (engine.webkit < 312) {
 69                     safariVersion = 1.2;
 70                 } else if (engine.webkit < 412) {
 71                     safariVersion = 1.3;
 72                 } else {
 73                     safariVersion = 2;
 74                 }
 75 
 76                 browser.safari = browser.ver = safariVersion;
 77             }
 78         } else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)) {
 79             engine.ver = browser.ver = RegExp["$1"];
 80             engine.khtml = browser.konq = parseFloat(engine.ver);
 81         } else if (/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)) {
 82             engine.ver = RegExp["$1"];
 83             engine.gecko = parseFloat(engine.ver);
 84 
 85             //check if firefox or not
 86             if (/Firefox\/(\S+)/.test(ua)) {
 87                 browser.ver = RegExp["$1"];
 88                 browser.firefox = parseFloat(browser.ver);
 89             }
 90         } else if (/MSIE ([^;]+)/.test(ua)) {
 91             engine.ver = browser.ver = RegExp["$1"];
 92             engine.ie = browser.ie = parseFloat(engine.ver);
 93         }
 94 
 95         //check browser
 96         browser.ie = engine.ie;
 97         browser.opera = engine.opera;
 98 
 99         //check platform
100         var p = navigator.platform;
101         system.win = p.indexOf("Win") == 0;
102         system.mac = p.indexOf("Mac") == 0;
103         system.xl1 = (p == "Xl1") || (p.indexOf("Linux") == 0);
104 
105         //check windows operate system
106         if (system.win) {
107             if (/Win(?:dows)?([^do]{2})\s?(\d+\.\d+)?/.test(ua)) {
108                 if (RegExp["$1"] == "NT") {
109                     switch (RegExp["$2"]) {
110                         case "5.0":
111                             system.win = "2000";
112                             break;
113                         case "5.1":
114                             system.win = "XP";
115                             break;
116                         case "6.0":
117                             system.win = "Vista";
118                             break;
119                         default:
120                             system.win = "NT";
121                             break;
122                     }
123                 } else if (RegExp["$1"] == "9x") {
124                     system.win = "ME";
125                 } else {
126                     system.win = RegExp["$1"];
127                 }
128             }
129         }
130 
131         //mobile device
132         system.iphone = ua.indexOf("iphone") > -1;
133         system.ipod = ua.indexOf("ipod") > -1;
134         system.nokiaN = ua.indexOf("NokiaN") > -1;
135         system.winMobile = (system.win == "CE");
136         system.macMobile = (system.iphone || system.ipod);
137 
138         //game system
139         system.wii = ua.indexOf("wii") > -1;
140         system.ps = /playstation/i.test(ua);
141 
142         //return these object
143         return {
144             engine: engine,
145             browser: browser,
146             system:system
147         };
148     } ();

 

posted on 2013-05-15 16:36  雷音  阅读(203)  评论(0编辑  收藏  举报

导航