1、首先判断pc端还是移动端。
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
};
//判断用户是在安卓还是苹果手机上打开设备
var u = navigator.userAgent;
if (u.indexOf(‘Android‘) > -1 || u.indexOf(‘Linux‘) > -1) {
console.log("设备安卓手机");
//安卓手机
} else if (u.indexOf(‘iPhone‘) > -1) {
console.log("设备为苹果手机");
//苹果手机
} else if (u.indexOf(‘Windows Phone‘) > -1) {
//winphone手机
console.log("3");
}
//判断用户是否在微信中打开页面
function isWeiXin(){
var ua = navigator.userAgent.toLowerCase();
if(ua.indexOf(‘micromessenger‘) != -1) {
console.log("是在微信中打开的页面");
return true;
} else {
console.log("不是在微信中打开的页面");
return false;
}
}
//判断用户是在什么设备中打开的页面
var events = navigator.userAgent;
console.log(navigator.userAgent);
console.log(navigator.appVersion);
console.log(navigator);
if(events.indexOf(‘Android‘)>-1 || events.indexOf(‘Linux‘)>-1 || events.indexOf(‘Adr‘)>-1){
console.log("安卓手机");
}else if(events.indexOf(‘iPhone‘)>-1){
//根据尺寸进行判断 苹果的型号
if(screen.height == 812 && screen.width == 375){
console.log("苹果X");
}else if(screen.height == 736 && screen.width == 414){
console.log("iPhone7P - iPhone8P - iPhone6");
}else if(screen.height == 667 && screen.width == 375){
console.log("iPhone7 - iPhone8 - iPhone6");
}else if(screen.height == 568 && screen.width == 320){
console.log("iPhone5");
}else{
console.log("iPhone4");
}
}else if(events.indexOf(‘Windows Phone‘)>-1){
console.log("诺基亚手机");
}else if(events.indexOf("iPad")>-1){
console.log("平板");
}
原文地址:https://www.cnblogs.com/MaTao123/p/8961036.html