Forever.Sun 判断当前系统,浏览器,版本号(PC&&移动端)

  1 var client = function () {
  2     //呈现引擎
  3     var engine = {
  4         ie: 0,
  5         gecko: 0,
  6         webkit: 0,
  7         khtml: 0,
  8         opera: 0,
  9         //完整版本号
 10         ver: null
 11     }
 12     //浏览器
 13     var browser = {
 14         //主要浏览器
 15         ie: 0,
 16         firefox: 0,
 17         safari: 0,
 18         konq: 0,
 19         opera: 0,
 20         chrome: 0,
 21         //具体版本号
 22         ver: null
 23     };
 24     //平台、设备和操作系统
 25     var system = {
 26         win: false,
 27         mac: false,
 28         x11: false,
 29         //移动设备
 30         iphone: false,
 31         ipod: false,
 32         ipad: false,
 33         ios: false,
 34         android: false,
 35         nokiaN: false,
 36         winMobile: false,
 37 
 38         //游戏系统
 39         wii: false,
 40         ps: false
 41     };
 42     //检测呈现引擎和浏览器
 43     var ua = navigator.userAgent;
 44     if (window.opera) {
 45         engine.ver = browser.ver = window.opera.version();
 46         engin.opera = browser.opera = parseFloat(engine.ver);
 47     } else if (/AppleWebKit\/(\S+)/.test(ua)) {
 48         engine.ver = RegExp["$1"];
 49         engine.webkit = parseFloat(engine.ver);
 50 
 51         //确定是chrome还是safari
 52         if (/Chrome\/(\S+)/.test(ua)) {
 53             browser.ver = RegExp["$1"];
 54             browser.chrome = parseFloat(browser.ver);
 55         } else if (/Version\/(\S+)/.test(ua)) {
 56             browser.ver == RegExp["$1"];
 57             browser.safari = parseFloat(browser.ver);
 58         } else {
 59             var safariVersion = 1;
 60             if (engine.webkit < 100) {
 61                 safariVersion = 1;
 62             } else if (engine.webkit < 312) {
 63                 safariVersion = 1.2;
 64             } else if (engine.webkit < 412) {
 65                 safariVersion = 1.3;
 66             } else {
 67                 safariVersion = 2;
 68             }
 69 
 70             browser.safari = browser.ver = safariVersion;
 71         }
 72     } else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)) {
 73         engine.ver = browser.ver = RegExp["$1"];
 74         engine.khtml = browser.konq = parseFloat(engine.ver);
 75     } else if (/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)) {
 76         engine.ver = RegExp["$1"];
 77         engine.gecko = parseFloat(engine.ver);
 78 
 79         //确定不是Firefox
 80         if (/Firefox\/(\S+)/.test(ua)) {
 81             browser.ver == RegExp["$1"];
 82             browser.firefox = parseFloat(browser.ver);
 83         }
 84     } else if (/MSIE ([^;]+)/.test(ua)) {
 85         engine.ver = browser.ver = RegExp["$1"];
 86         engine.ie = browser.ie = parseFloat(engine.ver);
 87     }
 88 
 89     //检测浏览器
 90     browser.ie = engine.ie;
 91     browser.opera = engine.opera;
 92 
 93     //检测平台
 94     var p = navigator.platform;
 95     system.win = p.indexOf("Win") == 0;
 96     system.mac = p.indexOf("Mac") == 0;
 97     system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
 98 
 99     //检测window操作系统
100     if (system.win) {
101         if (/Win(?:dows )?([^do]{2})\s?(\d+\.\d+)?/.test(ua)) {
102             if (RegExp["$1"] == "NT") {
103                 switch (RegExp["$2"]) {
104                     case "5.0":
105                         system.win = "2000";
106                         break;
107                     case "5.1":
108                         system.win = "XP";
109                         break;
110                     case "6.0":
111                         system.win = "Vista";
112                         break;
113                     case "6.1":
114                         system.win = "7";
115                         break;
116                     default:
117                         system.win = "NT";
118 
119                 }
120             } else if (RegExp["$1"] == "9x") {
121                 system.win = "ME";
122             } else {
123                 system.win = RegExp["$1"];
124             }
125         }
126     }
127 
128     //移动设备
129     system.iphone = ua.indexOf("iPhone") > -1;
130     system.ipod = ua.indexOf("iPod") > -1;
131     system.ipad = ua.indexOf("iPad") > -1;
132     system.nokiaN = ua.indexOf("NokiaN") > -1;
133 
134     //window mobile
135     if (system.win == "CE") {
136         system.winMobile = system.win;
137     } else if (system.win == "Ph") {
138         if (/windows Phone OS (\d+.\d+)/.test(ua)) {
139             system.win = "Phone";
140             system.winMobile = parseFloat(RegExp["$1"]);
141         }
142 
143     }
144 
145     //检测ios版本
146 
147     if (system.mac && ua.indexOf("Mobile") > -1) {
148         if (/CPU (?:iPhone )?OS (\d+_\d+)/.test(ua)) {
149             system.ios = parseFloat(RegExp.$1.replace("_", "."));
150         } else {
151             system.ios = 2;
152         }
153     }
154 
155     //检测Android版本
156     if (/Android (\d+\.\d+)/.test(ua)) {
157         system.android = parseFloat(RegExp.$1);
158     }
159 
160     //游戏系统
161     system.wii = ua.indexOf("Will") > -1;
162     system.ps = /playstation/i.test(ua);
163     //返回这些对象
164     return {
165         engine: engine,
166         browser: browser,
167         system: system
168     };
169 } ();

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。