1. 程式人生 > >【轉】通過js獲取系統版本以及瀏覽器版本

【轉】通過js獲取系統版本以及瀏覽器版本

返回 ati event 一個數 alt eve qq瀏覽器 搜狗瀏覽器 rms

技術分享圖片
  1 function getOsInfo() {
  2     var userAgent = navigator.userAgent.toLowerCase();
  3     var name = ‘Unknown‘;
  4     var version = "Unknown";
  5     if(userAgent.indexOf("win") > -1) {
  6         name = "Windows";
  7         if(userAgent.indexOf("windows nt 5.0") > -1) {
  8
version = "Windows 2000"; 9 } else if(userAgent.indexOf("windows nt 5.1") > -1 || userAgent.indexOf("windows nt 5.2") > -1) { 10 version = "Windows XP"; 11 } else if(userAgent.indexOf("windows nt 6.0") > -1) { 12 version = "Windows Vista";
13 } else if(userAgent.indexOf("windows nt 6.1") > -1 || userAgent.indexOf("windows 7") > -1) { 14 version = "Windows 7"; 15 } else if(userAgent.indexOf("windows nt 6.2") > -1 || userAgent.indexOf("windows 8") > -1) { 16 version = "Windows 8";
17 } else if(userAgent.indexOf("windows nt 6.3") > -1) { 18 version = "Windows 8.1"; 19 } else if(userAgent.indexOf("windows nt 6.2") > -1 || userAgent.indexOf("windows nt 10.0") > -1) { 20 version = "Windows 10"; 21 } else { 22 version = "Unknown"; 23 } 24 } else if(userAgent.indexOf("iphone") > -1) { 25 name = "Iphone"; 26 } else if(userAgent.indexOf("mac") > -1) { 27 name = "Mac"; 28 } else if(userAgent.indexOf("x11") > -1 || userAgent.indexOf("unix") > -1 || userAgent.indexOf("sunname") > -1 || userAgent.indexOf("bsd") > -1) { 29 name = "Unix"; 30 } else if(userAgent.indexOf("linux") > -1) { 31 if(userAgent.indexOf("android") > -1) { 32 name = "Android" 33 } else { 34 name = "Linux"; 35 } 36 37 } else { 38 name = "Unknown"; 39 } 40 var os = new Object(); 41 os.name = name; 42 os.version = version; 43 return os; 44 //document.write("系統:" + os.name + "版本:" + os.name) 45 } 46 function getBrowerInfo(){ 47 var Browser = Browser || (function(window) { 48 var document = window.document, 49 navigator = window.navigator, 50 agent = navigator.userAgent.toLowerCase(), 51 //IE8+支持.返回瀏覽器渲染當前文檔所用的模式 52 //IE6,IE7:undefined.IE8:8(兼容模式返回7).IE9:9(兼容模式返回7||8) 53 //IE10:10(兼容模式7||8||9) 54 IEMode = document.documentMode, 55 //chorme 56 chrome = window.chrome || false, 57 System = { 58 //user-agent 59 agent: agent, 60 //是否為IE 61 isIE: /trident/.test(agent), 62 //Gecko內核 63 isGecko: agent.indexOf("gecko") > 0 && agent.indexOf("like gecko") < 0, 64 //webkit內核 65 isWebkit: agent.indexOf("webkit") > 0, 66 //是否為標準模式 67 isStrict: document.compatMode === "CSS1Compat", 68 //是否支持subtitle 69 supportSubTitle: function() { 70 return "track" in document.createElement("track"); 71 }, 72 //是否支持scoped 73 supportScope: function() { 74 return "scoped" in document.createElement("style"); 75 }, 76 77 //獲取IE的版本號 78 ieVersion: function() { 79 var rMsie = /(msie\s|trident.*rv:)([\w.]+)/; 80 var ma = window.navigator.userAgent.toLowerCase() 81 var match = rMsie.exec(ma); 82 try { 83 return match[2]; 84 } catch (e) { 85 // console.log("error"); 86 return IEMode; 87 } 88 }, 89 //Opera版本號 90 operaVersion: function() { 91 try { 92 if (window.opera) { 93 return agent.match(/opera.([\d.]+)/)[1]; 94 } else if (agent.indexOf("opr") > 0) { 95 return agent.match(/opr\/([\d.]+)/)[1]; 96 } 97 } catch (e) { 98 // console.log("error"); 99 return 0; 100 } 101 } 102 }; 103 104 try { 105 //瀏覽器類型(IE、Opera、Chrome、Safari、Firefox) 106 System.type = System.isIE ? "IE" : 107 window.opera || (agent.indexOf("opr") > 0) ? "Opera" : 108 (agent.indexOf("chrome") > 0) ? "Chrome" : 109 //safari也提供了專門的判定方式 110 window.openDatabase ? "Safari" : 111 (agent.indexOf("firefox") > 0) ? "Firefox" : 112 ‘unknow‘; 113 114 //版本號 115 System.version = (System.type === "IE") ? System.ieVersion() : 116 (System.type === "Firefox") ? agent.match(/firefox\/([\d.]+)/)[1] : 117 (System.type === "Chrome") ? agent.match(/chrome\/([\d.]+)/)[1] : 118 (System.type === "Opera") ? System.operaVersion() : 119 (System.type === "Safari") ? agent.match(/version\/([\d.]+)/)[1] : 120 "0"; 121 122 //瀏覽器外殼 123 System.shell = function() { 124 125 if (agent.indexOf("edge") > 0) { 126 System.version = agent.match(/edge\/([\d.]+)/)[1] || System.version; 127 return "edge瀏覽器"; 128 } 129 //遨遊瀏覽器 130 if (agent.indexOf("maxthon") > 0) { 131 System.version = agent.match(/maxthon\/([\d.]+)/)[1] || System.version; 132 return "傲遊瀏覽器"; 133 } 134 //QQ瀏覽器 135 if (agent.indexOf("qqbrowser") > 0) { 136 System.version = agent.match(/qqbrowser\/([\d.]+)/)[1] || System.version; 137 return "QQ瀏覽器"; 138 } 139 140 //搜狗瀏覽器 141 if (agent.indexOf("se 2.x") > 0) { 142 return ‘搜狗瀏覽器‘; 143 } 144 145 //Chrome:也可以使用window.chrome && window.chrome.webstore判斷 146 if (chrome && System.type !== "Opera") { 147 var external = window.external, 148 clientInfo = window.clientInformation, 149 //客戶端語言:zh-cn,zh.360下面會返回undefined 150 clientLanguage = clientInfo.languages; 151 152 //獵豹瀏覽器:或者agent.indexOf("lbbrowser")>0 153 if (external && ‘LiebaoGetVersion‘ in external) { 154 return ‘獵豹瀏覽器‘; 155 } 156 //百度瀏覽器 157 if (agent.indexOf("bidubrowser") > 0) { 158 System.version = agent.match(/bidubrowser\/([\d.]+)/)[1] || 159 agent.match(/chrome\/([\d.]+)/)[1]; 160 return "百度瀏覽器"; 161 } 162 //360極速瀏覽器和360安全瀏覽器 163 if (System.supportSubTitle() && typeof clientLanguage === "undefined") { 164 //object.key()返回一個數組.包含可枚舉屬性和方法名稱 165 var storeKeyLen = Object.keys(chrome.webstore).length, 166 v8Locale = "v8Locale" in window; 167 return storeKeyLen > 1 ? ‘360極速瀏覽器‘ : ‘360安全瀏覽器‘; 168 } 169 return "Chrome"; 170 } 171 return System.type; 172 }; 173 174 //瀏覽器名稱(如果是殼瀏覽器,則返回殼名稱) 175 System.name = System.shell(); 176 //對版本號進行過濾過處理 177 // System.version = System.versionFilter(System.version); 178 179 } catch (e) { 180 // console.log(e.message); 181 } 182 return { 183 client: System 184 }; 185 186 })(window); 187 if (Browser.client.name == undefined || Browser.client.name=="") { 188 Browser.client.name = "Unknown"; 189 Browser.client.version = "Unknown"; 190 }else if(Browser.client.version == undefined){ 191 Browser.client.version = "Unknown"; 192 } 193 // document.write(Browser.client.name + " " + Browser.client.version); 194 return Browser ; 195 }
View Code

自己備用

【轉】通過js獲取系統版本以及瀏覽器版本