-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS 常用的判断或校验
119 lines (87 loc) · 3.21 KB
/
JS 常用的判断或校验
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
1、判断是Android终端还是iOS终端
let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //判断是否是 android终端
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //判断是否是 iOS终端
—— 封装成方法:
/*判断客户端*/
judgeClient() {
let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //判断是否是 android终端
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //判断是否是 iOS终端
console.log('是否是Android:' + isAndroid); //true,false
console.log('是否是iOS:' + isIOS);
if(isAndroid){
return 'Android';
}else if(isIOS){
return 'IOS';
}else{
return 'PC';
}
},
2、判断是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)) { //判断iPhone|iPad|iPod|iOS
client = 'iOS';
} else if (/(Android)/i.test(navigator.userAgent)) { //判断Android
client = 'Android';
} else {
client = 'PC';
}
sreturn client;
},
3、判断PC还是移动端
isPC() {
/*true则pc,false则mobile*/
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;
}
4、判断多种访问终端
//判断访问终端
let browser = {
versions: function () {
let u = navigator.userAgent, app = navigator.appVersion;
return {
trident: u.indexOf('Trident') > -1, //IE内核
presto: u.indexOf('Presto') > -1, //opera内核
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('Adr') > -1, //android终端
iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1, //是否iPad
webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
weixin: u.indexOf('MicroMessenger') > -1, //是否微信
qq: u.match(/\sQQ/i) == " qq" //是否QQ
};
}(),
language: (navigator.browserLanguage || navigator.language).toLowerCase()
};
5、判断浏览器当前使用的语言
judgeLanguage() {
// 判断浏览器当前使用的语言
let currentLanguage = (navigator.browserLanguage || navigator.language).toLowerCase(); // 非IE
if (!currentLanguage) { // IE浏览器
currentLanguage = navigator.browserLanguage;
}
console.log(currentLanguage);
return currentLanguage;
},