javascript实现获取浏览器版本、操作系统类型
作者:一起走过的日子 时间:2024-05-13 10:36:21
代码很简洁,功能很实用,这里就不多废话了,直接给大家奉上代码:
/**
* Created by Administrator on 15-1-12.
*/
function BroswerUtil() {
}
BroswerUtil = {
//检测浏览器版本
getBrowserVersion: function () {
var agent = navigator.userAgent.toLowerCase();
var arr = [];
var Browser = "";
var Bversion = "";
var verinNum = "";
//IE
if (agent.indexOf("msie") > 0) {
var regStr_ie = /msie [\d.]+;/gi;
Browser = "IE";
Bversion = "" + agent.match(regStr_ie)
}
//firefox
else if (agent.indexOf("firefox") > 0) {
var regStr_ff = /firefox\/[\d.]+/gi;
Browser = "firefox";
Bversion = "" + agent.match(regStr_ff);
}
//Chrome
else if (agent.indexOf("chrome") > 0) {
var regStr_chrome = /chrome\/[\d.]+/gi;
Browser = "chrome";
Bversion = "" + agent.match(regStr_chrome);
}
//Safari
else if (agent.indexOf("safari") > 0 && agent.indexOf
("chrome") < 0) {
var regStr_saf = /version\/[\d.]+/gi;
Browser = "safari";
Bversion = "" + agent.match(regStr_saf);
}
//Opera
else if (agent.indexOf("opera") >= 0) {
var regStr_opera = /version\/[\d.]+/gi;
Browser = "opera";
Bversion = "" + agent.match(regStr_opera);
} else {
var browser = navigator.appName;
if (browser == "Netscape") {
var version = agent.split(";");
var trim_Version = version[7].replace(/[ ]/g,
"");
var rvStr = trim_Version.match(/
[\d\.]/g).toString();
var rv = rvStr.replace(/[,]/g, "");
Bversion = rv;
Browser = "IE"
}
}
verinNum = (Bversion + "").replace(/[^0-9.]/ig, "");
arr.push(Browser);
arr.push(verinNum);
return arr;
},
//检测是否是XX浏览器
WB: (function () {
var UserAgent = navigator.userAgent.toLowerCase();
return {
isIE6: /msie 6.0/.test(UserAgent), // IE6
isIE7: /msie 7.0/.test(UserAgent), // IE7
isIE8: /msie 8.0/.test(UserAgent), // IE8
isIE9: /msie 9.0/.test(UserAgent), // IE9
isIE10: /msie 10.0/.test(UserAgent), // IE10
isIE11: /msie 11.0/.test(UserAgent), // IE11
isLB: /lbbrowser/.test(UserAgent), // 猎豹浏览器
isUc: /ucweb/.test(UserAgent), // UC浏览器
is360: /360se/.test(UserAgent), // 360浏览器
isBaidu: /bidubrowser/.test(UserAgent), // 百度浏览器
isSougou: /metasr/.test(UserAgent), // 搜狗浏览器
isChrome: /chrome/.test(UserAgent.substr(-33, 6)), //
Chrome浏览器
isFirefox: /firefox/.test(UserAgent), // 火狐浏览器
isOpera: /opera/.test(UserAgent), // Opera浏览器
isSafire: /safari/.test(UserAgent) && !/chrome/.test
(UserAgent), // safire浏览器
isQQ: /qqbrowser/.test(UserAgent)//qq浏览器
};
})(),
//检测当前操作系统
CurrentSystem: (function () {
var system = {
win: false,
mac: false,
xll: false,
iphone: false,
ipoad: false,
ipad: false,
ios: false,
android: false,
nokiaN: false,
winMobile: false,
wii: false,
ps: false
};
var ua = navigator.userAgent;
// 检测平台
var p = navigator.platform;
system.win = p.indexOf('Win') == 0;
system.mac = p.indexOf('Mac') == 0;
system.xll = (p.indexOf('Xll') == 0 || p.indexOf('Linux')
== 0);
// 检测Windows操作系统
if (system.win) {
if (/Win(?:dows )?([^do]{2})\s?(\d+\.\d+)?/.test(ua))
{
if (RegExp['$1'] == 'NT') {
switch (RegExp['$2']) {
case '5.0':
system.win = '2000';
break;
case '5.1':
system.win = 'XP';
break;
case '6.0':
system.win = 'Vista';
break;
case '6.1':
system.win = '7';
break;
case '6.2':
system.win = '8';
break;
default:
system.win = 'NT';
break;
}
} else if (RegExp['$1'] == '9x') {
system.win = 'ME';
} else {
system.win = RegExp['$1'];
}
}
}
// 移动设备
system.iphone = ua.indexOf('iPhone') > -1;
system.ipod = ua.indexOf('iPod') > -1;
system.ipad = ua.indexOf('iPad') > -1;
system.nokiaN = ua.indexOf('nokiaN') > -1;
// windows mobile
if (system.win == 'CE') {
system.winMobile = system.win;
} else if (system.win == 'Ph') {
if (/Windows Phone OS (\d+.\d)/i.test(ua)) {
system.win = 'Phone';
system.winMobile = parseFloat(RegExp['$1']);
}
}
// 检测IOS版本
if (system.mac && ua.indexOf('Mobile') > -1) {
if (/CPU (?:iPhone )?OS (\d+_\d+)/i.test(ua)) {
system.ios = parseFloat(RegExp['$1'].replace('_',
'.'));
} else {
system.ios = 2; // 不能真正检测出来,所以
只能猜测
}
}
// 检测Android版本
if (/Android (\d+\.\d+)/i.test(ua)) {
system.android = parseFloat(RegExp['$1']);
}
// 游戏系统
system.wii = ua.indexOf('Wii') > -1;
system.ps = /PlayStation/i.test(ua);
return {
system: system
}
})()
}
标签:javascript,浏览器版本,操作系统
0
投稿
猜你喜欢
Django数据库类库MySQLdb使用详解
2024-01-14 01:26:20
深入谈谈MySQL中的自增主键
2024-01-15 13:24:10
兼容主流浏览器,纯CSS下拉菜单
2010-09-05 20:30:00
python定义类self用法实例解析
2022-01-08 14:11:01
Python的Django框架中的URL配置与松耦合
2022-11-19 10:23:33
Python使用pytest-playwright的原因分析
2023-12-12 01:22:39
细化解析:轻松掌握怎样测试 MySQL安装
2009-01-14 11:54:00
python递归全排列实现方法
2021-11-17 15:16:04
Mybatis出现ORA-00911: invalid character的解决办法
2024-01-19 02:41:21
如何用Python识别车牌的示例代码
2023-03-09 16:06:29
对python字典过滤条件的实例详解
2022-01-28 14:02:30
Python命令行定时任务自动化工作流程
2023-01-01 20:52:51
数据分页显示按钮,自动获取url链接
2009-06-22 12:59:00
asp随机数 随机产生N位由数字和字母组成的密码
2011-03-10 10:47:00
python自定义线程池控制线程数量的示例
2022-12-25 15:16:13
Windows下Anaconda下载安装与配置教程分享
2023-09-29 22:37:11
使用numpy.mean() 计算矩阵均值方式
2021-12-17 03:55:53
收缩后对数据库的使用有影响吗?
2024-01-21 09:41:48
浅谈一下关于Python对XML的解析
2023-08-15 21:33:36
python3 queue多线程通信
2022-09-20 08:41:05