Last active
December 17, 2015 06:38
-
-
Save RockyMyx/5566431 to your computer and use it in GitHub Desktop.
JavaScript: detectBrowser-ppk.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From : http://www.quirksmode.org/js/detect.html | |
var BrowserDetect = { | |
init: function() { | |
this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; | |
//navigator.userAgent may not contain the string "iCab", but navigator.appVersion always does. | |
this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; | |
this.OS = this.searchString(this.dataOS) || "an unknown OS"; | |
}, | |
searchString: function(data) { | |
for (var i = 0; i < data.length; i++) { | |
var dataString = data[i].string; | |
var dataProp = data[i].prop; | |
this.versionSearchString = data[i].versionSearch || data[i].identity; | |
if (dataString) { | |
if (dataString.indexOf(data[i].subString) != -1) return data[i].identity; | |
} else if (dataProp) return data[i].identity; | |
} | |
}, | |
searchVersion: function(dataString) { | |
//Safari's string never contains its official version; only its internal Apple version number (ie. not 1.3.2 but 312.6). Therefore the version number detect doesn't work in Safari. | |
var index = dataString.indexOf(this.versionSearchString); | |
if (index == -1) return; | |
return parseFloat(dataString.substring(index + this.versionSearchString.length + 1)); | |
}, | |
dataBrowser: [{ | |
string: navigator.userAgent, | |
subString: "Chrome", | |
identity: "Chrome" | |
}, { | |
string: navigator.userAgent, | |
subString: "OmniWeb", | |
versionSearch: "OmniWeb/", | |
identity: "OmniWeb" | |
}, { | |
//Safari's navigator.userAgent also contains "Gecko". This causes the same problem: a check for Mozilla would reveal "Gecko", and Safari would be identified as Mozilla. Therefore the Mozilla check only takes place if the browser is not Safari. | |
string: navigator.vendor, | |
subString: "Apple", | |
identity: "Safari", | |
versionSearch: "Version" | |
}, { | |
// the Opera navigator.userAgent may contain "MSIE". If we'd check for Explorer first, we'd find the "MSIE" and would incorrectly conclude that the browser is Explorer. In order to avoid this false detection, we should check for Opera first. If the browser is in fact Opera, the script never proceeds to the "MSIE" check. | |
prop: window.opera, | |
identity: "Opera", | |
versionSearch: "Version" | |
}, { | |
string: navigator.vendor, | |
subString: "iCab", | |
identity: "iCab" | |
}, { | |
string: navigator.vendor, | |
subString: "KDE", | |
identity: "Konqueror" | |
}, { | |
string: navigator.userAgent, | |
subString: "Firefox", | |
identity: "Firefox" | |
}, { | |
string: navigator.vendor, | |
subString: "Camino", | |
identity: "Camino" | |
}, { | |
// for newer Netscapes (6+) | |
string: navigator.userAgent, | |
subString: "Netscape", | |
identity: "Netscape" | |
}, { | |
//If you add a new object to detect a new browser, always add it before the Explorer/Mozilla objects at the end of the array. | |
string: navigator.userAgent, | |
subString: "MSIE", | |
identity: "Explorer", | |
versionSearch: "MSIE" | |
}, { | |
string: navigator.userAgent, | |
subString: "Gecko", | |
identity: "Mozilla", | |
versionSearch: "rv" | |
}, { // for older Netscapes (4-) | |
string: navigator.userAgent, | |
subString: "Mozilla", | |
identity: "Netscape", | |
versionSearch: "Mozilla" | |
}], | |
dataOS: [{ | |
string: navigator.platform, | |
subString: "Win", | |
identity: "Windows" | |
}, { | |
string: navigator.platform, | |
subString: "Mac", | |
identity: "Mac" | |
}, { | |
string: navigator.userAgent, | |
subString: "iPhone", | |
identity: "iPhone/iPod" | |
}, { | |
string: navigator.platform, | |
subString: "Linux", | |
identity: "Linux" | |
}] | |
}; | |
BrowserDetect.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment