blob: f5f0f6409b782bd7a4be0e2955875bf67276eb99 [file] [log] [blame]
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -08001function detect() {
2
3 var nVer = navigator.appVersion;
4 var nAgt = navigator.userAgent;
5 var browserName = navigator.appName;
6 var fullVersion = '' + parseFloat(navigator.appVersion);
7 var majorVersion = parseInt(navigator.appVersion, 10);
8 var nameOffset, verOffset, ix;
9
10 // In Opera, the true version is after "Opera" or after "Version"
11 if ((verOffset = nAgt.indexOf("Opera")) != -1) {
12 browserName = "Opera";
13 fullVersion = nAgt.substring(verOffset + 6);
14 if ((verOffset = nAgt.indexOf("Version")) != -1) fullVersion = nAgt.substring(verOffset + 8);
15 }
16 // In MSIE, the true version is after "MSIE" in userAgent
17 else if ((verOffset = nAgt.indexOf("MSIE")) != -1) {
18 browserName = "Microsoft Internet Explorer";
19 fullVersion = nAgt.substring(verOffset + 5);
20 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080021 // In Chrome, the true version is after "Chrome"
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080022 else if ((verOffset = nAgt.indexOf("Chrome")) != -1) {
23 browserName = "Chrome";
24 fullVersion = nAgt.substring(verOffset + 7);
25 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080026 // In Safari, the true version is after "Safari" or after "Version"
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080027 else if ((verOffset = nAgt.indexOf("Safari")) != -1) {
28 browserName = "Safari";
29 fullVersion = nAgt.substring(verOffset + 7);
30 if ((verOffset = nAgt.indexOf("Version")) != -1) fullVersion = nAgt.substring(verOffset + 8);
31 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080032 // In Firefox, the true version is after "Firefox"
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080033 else if ((verOffset = nAgt.indexOf("Firefox")) != -1) {
34 browserName = "Firefox";
35 fullVersion = nAgt.substring(verOffset + 8);
36 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080037 // In most other browsers, "name/version" is at the end of userAgent
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080038 else if ((nameOffset = nAgt.lastIndexOf(' ') + 1) < (verOffset = nAgt.lastIndexOf('/'))) {
39 browserName = nAgt.substring(nameOffset, verOffset);
40 fullVersion = nAgt.substring(verOffset + 1);
41 if (browserName.toLowerCase() == browserName.toUpperCase()) {
42 browserName = navigator.appName;
43 }
44 }
45 // trim the fullVersion string at semicolon/space if present
46 if ((ix = fullVersion.indexOf(";")) != -1) fullVersion = fullVersion.substring(0, ix);
47 if ((ix = fullVersion.indexOf(" ")) != -1) fullVersion = fullVersion.substring(0, ix);
48
49 majorVersion = parseInt('' + fullVersion, 10);
50 if (isNaN(majorVersion)) {
51 fullVersion = '' + parseFloat(navigator.appVersion);
52 majorVersion = parseInt(navigator.appVersion, 10);
53 }
54
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080055 if (browserName == "Safari" && fullVersion == "5.1.7") {
56 return false;
57 } else {
58 return true;
59 }
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080060}