-
通過瀏覽器的 navigator.userAgent 判斷是Android還是IOS:
一.判斷是Android還是iOS
let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
console.log('是否是Android:' + isAndroid);
console.log('是否是iOS:' + isIOS);
複製程式碼
judgeClient() {
let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
console.log('是否是Android:' + isAndroid);
console.log('是否是iOS:' + isIOS);
if(isAndroid === true){
return 'Android';
}else if(isIOS === true){
return 'IOS';
}else{
return 'PC';
}
},
複製程式碼
二.判斷是Android還是iOS
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
console.log('isIOS');
} else if (/(Android)/i.test(navigator.userAgent)) {
console.log('isAndroid');
} else {
console.log('isPC');
}
複製程式碼
judgeClient() {
let client = '';
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
client = 'IOS';
} else if (/(Android)/i.test(navigator.userAgent)) {
client = 'Android';
} else {
client = 'PC';
}
return client;
},
複製程式碼
三.判斷PC還是移動端
isPC: function () {
let u = navigator.userAgent;
let Agents = ["Android", "iPhone", "webOS", "BlackBerry", "SymbianOS", "Windows Phone", "iPad", "iPod"];
let flag = true;
for (let i = 0; i < Agents.length; i++) {
if (u.indexOf(Agents[i]) > 0) {
flag = false;
break;
}
}
return flag;
},
複製程式碼
四.判斷多種訪問終端
let browser = {
versions: function () {
let u = navigator.userAgent, app = navigator.appVersion;
return {
trident: u.indexOf('Trident') > -1,
presto: u.indexOf('Presto') > -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/),
android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1,
iPhone: u.indexOf('iPhone') > -1,
iPad: u.indexOf('iPad') > -1,
webApp: u.indexOf('Safari') == -1,
weixin: u.indexOf('MicroMessenger') > -1,
qq: u.match(/\sQQ/i) == " qq"
};
}(),
language: (navigator.browserLanguage || navigator.language).toLowerCase()
};
複製程式碼
if (browser.versions.mobile) {
console.log("is mobile");
}
if (browser.versions.ios) {
console.log("is ios");
}
複製程式碼
五.判斷瀏覽器當前使用的語言
judgeLanguage() {
let currentLanguage = (navigator.browserLanguage || navigator.language).toLowerCase();
if (!currentLanguage) {
currentLanguage = navigator.browserLanguage;
}
console.log(currentLanguage);
return currentLanguage;
},
複製程式碼