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