利用navigator.userAgent获取的设备信息,后用.test来提取字符获取项目运行的设备系统及浏览器
//判断系统
userAgent() {
//如果是电脑,则判断电脑的操作系统
function detectOS() {
let sUserAgent = navigator.userAgent;
let isWin =
navigator.platform == "Win32" || navigator.platform == "Windows";
let isMac =
navigator.platform == "Mac68K" ||
navigator.platform == "MacPPC" ||
navigator.platform == "Macintosh" ||
navigator.platform == "MacIntel";
if (isMac) return "Mac";
let isUnix = navigator.platform == "X11" && !isWin && !isMac;
if (isUnix) return "Unix";
let isLinux = String(navigator.platform).indexOf("Linux") > -1;
if (isLinux) return "Linux";
if (isWin) {
let isWin2K =
sUserAgent.indexOf("Windows NT 5.0") > -1 ||
sUserAgent.indexOf("Windows 2000") > -1;
if (isWin2K) return "Win2000";
let isWinXP =
sUserAgent.indexOf("Windows NT 5.1") > -1 ||
sUserAgent.indexOf("Windows XP") > -1;
if (isWinXP) return "WinXP";
let isWin2003 =
sUserAgent.indexOf("Windows NT 5.2") > -1 ||
sUserAgent.indexOf("Windows 2003") > -1;
if (isWin2003) return "Win2003";
let isWinVista =
sUserAgent.indexOf("Windows NT 6.0") > -1 ||
sUserAgent.indexOf("Windows Vista") > -1;
if (isWinVista) return "WinVista";
let isWin7 =
sUserAgent.indexOf("Windows NT 6.1") > -1 ||
sUserAgent.indexOf("Windows 7") > -1;
if (isWin7) return "Win7";
let isWin8 =
sUserAgent.indexOf("Windows NT 8.0") > -1 ||
sUserAgent.indexOf("Windows 8") > -1;
if (isWin8) return "Win8";
let isWin10 =
sUserAgent.indexOf("Windows NT 10.0") > -1 ||
sUserAgent.indexOf("Windows 10") > -1;
if (isWin10) return "Win10";
}
return "other";
}
alert("您的操作系统是:" + detectOS());
//判断是手机还是电脑,什么浏览器,手机操作系统
let browser = (function () {
let ua = navigator.userAgent;
let isWindowsPhone = /(?:Windows Phone)/.test(ua);
let isSymbian = /(?:SymbianOS)/.test(ua);
let isAndroid = /(?:Android)/.test(ua);
let isFireFox = /(?:Firefox)/.test(ua);
let isTablet = /(?:iPad|PlayBook)/.test(ua) ||
(isAndroid && !/(?:Mobile)/.test(ua)) ||
(isFireFox && /(?:Tablet)/.test(ua));
let isPhone = /(?:iPhone)/.test(ua) && !isTablet;
let isChrome = /(?:Chrome|CriOS)/.test(ua);
let WX = /(?:MicroMessenger)/.test(ua);
let isMobile = isWindowsPhone || isSymbian || isAndroid || isPhone;
let isPc = !isMobile;
return {
isMobile: isMobile,
isPc: isPc,
isPhone: isPhone, //苹果
isWindowsPhone: isWindowsPhone, //微软手机
isSymbian: isSymbian, //班赛
isAndroid: isAndroid, //安卓
isFireFox: isFireFox, //火狐
isChrome: isChrome, //谷歌
isTablet: isTablet, //平板
WX: WX //微信
};
})();
let visit_type; //手机 电脑 平板
let userBrowser; //浏览器
let userOs; //操作系统
if (browser.isPc) {
//电脑
visit_type = "pc";
//如果是电脑则判断其操作系统
userOs = detectOS();
if (browser.isFireFox) {
userBrowser = "火狐";
} else if (browser.isChrome) {
userBrowser = "谷歌";
} else {
userBrowser = "ie";
}
} else if (browser.isMobile) {
//手机
visit_type = "手机";
userBrowser = "手机应用";
if (browser.isPhone) {
userOs = "苹果";
} else if (browser.isWindowsPhone) {
userOs = "微软";
} else if (browser.isSymbian) {
userOs = "塞班";
} else if (browser.isAndroid) {
userOs = "安卓";
} else {
userOs = "其他";
}
} else if (browser.isTablet) {
this.userOs = "app";//因为项目原因统一是归类app端
visit_type = "平板";
userOs = "其他";
userBrowser = "其他";
} else {
this.userOs = "app";//因为项目原因统一是归类app端
visit_type = "其他";
userOs = "其他";
userBrowser = "其他";
}
//判断手机系统,项目原因竟分类 iOS和安卓两端
if(userOs == '苹果'){
this.userOsZ = '苹果'
} else {
this.userOsZ = '安卓'
}
//判断是否未微信浏览器(因为微信浏览器兼容问题比较多,特别的iOS)
if(browser.WX){
this.IOS_wx = true
}
},