【转】通过js获取系统版本以及浏览器版本

  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

 自己备用

posted @ 2018-08-02 12:55  § 薄樱 §  阅读(534)  评论(0编辑  收藏  举报