是否是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;
}
获取当前浏览器的内核类型
function GetMobelType() {
var browser = {
versions: function () {
var u = window.navigator.userAgent;
var ua = navigator.userAgent.toLowerCase();
console.log(u); //Safari浏览器 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15
return {
trident: u.indexOf('Trident') > -1, //IE内核
presto: u.indexOf('Presto') > -1, //opera内核
Alipay: u.indexOf('Alipay') > -1, //支付宝
webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者安卓QQ浏览器
//iPhone: u.match(/iphone|ipod|ipad/),//
iPad: u.indexOf('iPad') > -1, //是否为iPad
webApp: u.indexOf('Safari') == -1, //是否为web应用程序,没有头部与底部
// weixin: u.indexOf('MicroMessenger') > -1, //是否为微信浏览器
weixin: ua.match(/MicroMessenger/i) && ua.match(/MicroMessenger/i).includes("micromessenger"),
// qq: u.match(/\sQQ/i) !== null, //是否QQ
qq: ua.match(/QQ/i) && ua.match(/QQ/i).includes("qq"),
Safari: u.indexOf('Safari') > -1,
///Safari浏览器,
};
}()
};
return browser.versions;
}
if (GetMobelType().mobile) {
console.log('mobile');
//移动端
if (GetMobelType().android) {
if (GetMobelType().weixin || GetMobelType().qq) {
} else {
}
}
if (GetMobelType().ios || GetMobelType().iPhone || GetMobelType().iPad) {
if (GetMobelType().weixin || GetMobelType().qq) { // ios的苹果浏览器
} else {
// alert('mobile ios/iphone/ipad not qq');
};
}
} else {
//pc端
console.log('pc');
}