Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 1 | function 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 21 | // In Chrome, the true version is after "Chrome" |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 22 | else if ((verOffset = nAgt.indexOf("Chrome")) != -1) { |
| 23 | browserName = "Chrome"; |
| 24 | fullVersion = nAgt.substring(verOffset + 7); |
| 25 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 26 | // In Safari, the true version is after "Safari" or after "Version" |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 27 | 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 32 | // In Firefox, the true version is after "Firefox" |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 33 | else if ((verOffset = nAgt.indexOf("Firefox")) != -1) { |
| 34 | browserName = "Firefox"; |
| 35 | fullVersion = nAgt.substring(verOffset + 8); |
| 36 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 37 | // In most other browsers, "name/version" is at the end of userAgent |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 38 | 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 55 | if (browserName == "Safari" && fullVersion == "5.1.7") { |
| 56 | return false; |
| 57 | } else { |
| 58 | return true; |
| 59 | } |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 60 | } |