【转】通过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 }

自己备用

原文地址:https://www.cnblogs.com/CherryGhost/p/9406540.html

时间: 2024-08-27 12:54:11

【转】通过js获取系统版本以及浏览器版本的相关文章

js获取系统月份大小总是小一位

一 问题:项目过程中用一下方法获取系统日期,结果月份总是小一位. var myDate = new Date();myDate.getFullYear();myDate.getMonth();$("#date1").val(myDate.getFullYear() + '/' + myDate.getMonth()); 二  解决方法  查询相关资料后得知JS获取系统月份getMonth() 是0-11来表示1-12的.最后将项目源码最后行改成 $("#date1"

原生JS获取DOM 节点到浏览器顶部的距离或者左侧的距离

关于js获取dom 节点到浏览器顶/左部的距离,Jquery里面有封装好的offset().top/offset().left,只到父级的顶/左部距离position().top/position().left: 原生写的话就是用获取节点,do while循环就可以了.代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta

Js获取操作系统版本 &amp;&amp; 获得浏览器版本

//利用原生Js获取操作系统版本function getOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows"); var isMac = (navigator.platform == "Mac68K") || (navigator.platform ==

JS获取当前使用的浏览器名字以及版本号

工作中需要通过JS去获取当前使用的浏览器的名字以及版本号,网上大堆资料都有一个关键词是 navigator.appName,但是这个方法获取的浏览器的名字只有两种要么是IE要么就是Netscap,倒是可以用来判断是否使用了IE,但是我想获取具体的浏览器产品名字比如  Firefox,Chrome等.所以只好通过navigator.userAgent,但是这个字符串是非常长的,分析他的特征,通过正则表达式来解决这个问题是不错的方法. (1) 获取浏览器名字+版本字符串 function getBr

js 获取系统字体

参考自网上,非原创. 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 </head> 6 <body> 7 <div style="left: 0px; positon: absolute; top: 0px;

js获取页面元素距离浏览器工作区顶端的距离

先介绍几个属性:(暂时只测了IE和firefox,实际上我工作中用到的最多的是chrome) 网页被卷起来的高度/宽度(即浏览器滚动条滚动后隐藏的页面内容高度) (javascript)        document.documentElement.scrollTop //firefox (javascript)        document.documentElement.scrollLeft //firefox (javascript)        document.body.scro

android获取系统自带浏览器书签

刚刚接手一个备份系统浏览器书签的模块,现在把代码贴出来,另外有几点疑问请路过的大神指教 1.根据官方api应该是有以下几个字段是可以获取的 但是除了TITLE和URL能获取其他的都会报java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.异

js 获取系统当前时间

JS获取当前的日期和时间的方法:var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1970-????)myDate.getMonth(); //获取当前月份(0-11,0代表1月)myDate.getDate(); //获取当前日(1-31)myDate.getDay(); //获取当前星期X(0-6,0代表星期天)myDate.getTime(); //获取当前时间(

js网页判断移动终端浏览器版本信息是安卓还是苹果ios,判断在微信浏览器跳转不同页面,生成二维码

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>移动终端浏览器版本信息</title> </head> <body> <script type="text/javascript"> var browser = { versions: function () { var u = naviga