今天用到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
|
/* * 描述:判断浏览器信息 * 编写:LittleQiang_w * 日期:2016.1.5 * 版本:V1.1 */ //判断当前浏览类型 function BrowserType() { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var isOpera = userAgent.indexOf( "Opera" ) > -1; //判断是否Opera浏览器 var isIE = userAgent.indexOf( "compatible" ) > -1 && userAgent.indexOf( "MSIE" ) > -1 && !isOpera; //判断是否IE浏览器 var isEdge = userAgent.indexOf( "Windows NT 6.1; Trident/7.0;" ) > -1 && !isIE; //判断是否IE的Edge浏览器 var isFF = userAgent.indexOf( "Firefox" ) > -1; //判断是否Firefox浏览器 var isSafari = userAgent.indexOf( "Safari" ) > -1 && userAgent.indexOf( "Chrome" ) == -1; //判断是否Safari浏览器 var isChrome = userAgent.indexOf( "Chrome" ) > -1 && userAgent.indexOf( "Safari" ) > -1; //判断Chrome浏览器 if (isIE) { var reIE = new RegExp( "MSIE (\d+\.\d+);" ); reIE.test(userAgent); var fIEVersion = parseFloat(RegExp[ "$1" ]); if (fIEVersion == 7) { return "IE7" ;} else if (fIEVersion == 8) { return "IE8" ;} else if (fIEVersion == 9) { return "IE9" ;} else if (fIEVersion == 10) { return "IE10" ;} else if (fIEVersion == 11) { return "IE11" ;} else { return "0" } //IE版本过低 } //isIE end if (isFF) { return "FF" ;} if (isOpera) { return "Opera" ;} if (isSafari) { return "Safari" ;} if (isChrome) { return "Chrome" ;} if (isEdge) { return "Edge" ;} } //myBrowser() end //判断是否是IE浏览器 function isIE() { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var isIE = userAgent.indexOf( "compatible" ) > -1 && userAgent.indexOf( "MSIE" ) > -1 && !isOpera; //判断是否IE浏览器 if (isIE) { return "1" ; } else { return "-1" ; } } //判断是否是IE浏览器,包括Edge浏览器 function IEVersion() { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var isIE = userAgent.indexOf( "compatible" ) > -1 && userAgent.indexOf( "MSIE" ) > -1 && !isOpera; //判断是否IE浏览器 var isEdge = userAgent.indexOf( "Windows NT 6.1; Trident/7.0;" ) > -1 && !isIE; //判断是否IE的Edge浏览器 if (isIE) { var reIE = new RegExp( "MSIE (\d+\.\d+);" ); reIE.test(userAgent); var fIEVersion = parseFloat(RegExp[ "$1" ]); if (fIEVersion == 7) { return "IE7" ;} else if (fIEVersion == 8) { return "IE8" ;} else if (fIEVersion == 9) { return "IE9" ;} else if (fIEVersion == 10) { return "IE10" ;} else if (fIEVersion == 11) { return "IE11" ;} else { return "0" } //IE版本过低 } else if (isEdge) { return "Edge" ; } else { return "-1" ; //非IE } } |
以上代码通过测试,可用!
但存在一个问题,即IE5与IE7浏览器暂不能区分,以下是IE5与IE7下userAgent的信息;通过userAgent发现,简单的通过以上方法,无法正确区分IE5和IE7。期待该问题的解决方案!!!
IE5: userAgent "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.2; .NET4.0E)"
IE7: userAgent "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.2; .NET4.0E)"