To add to this discussion. I just came up with a jquery $.browser plugin that, instead of 'detection', just merely parses the user agent into an easy-to-use object. Further logic can easily be applied to further break down and parse specific browsers and platforms.
I have had very reliable results on useragents found here: UserAgentString.com. I even included the version detection for ie11 (near the bottom).
//The following code is by no means perfect, nor is it meant to be a standalone 'detection' plugin.
//It demonstrates parsing the useragent string into an easy to manage object.
//Even if it does make detection rediculously easy.. :)
//Because this regex makes no assumptions in advance.
//IMO, It's compatibilty and maintainability is much higher than those based on static identifiers.
/*
uaMatch replacement that parses a useragent string into an object
useragent segments can be Name Value OR Name/Value OR Name
Segment: Name Value
Name: parsed to the last whitespace character
Value: value after the last whitespace character
Matches: (.NET CLR) (#.##), Android 2.3.4, etc
Note: this regex can have leading/trailing whitespace (trimmed for object properties)
Segment: Name/Value
Matches: all values matching Name/Value
Example: Firefox/24.0, Safari/533.1, Version/12.1, etc
Segment: Name
Matches: identifiers that hold no values (value of 'true' is implied)
Example: Macintosh, Linux, Windows, KHTML, U, etc
WARNING: not necessarily compatible with jQuery's $.browser implementation.
- not recommended as a replacement for plugins that require it to function.
*/
(function ($) {
var ua = navigator.userAgent.toLowerCase();
var regex = /compatible; ([\w.+]+)[ \/]([\w.+]*)|([\w .+]+)[: \/]([\w.+]+)|([\w.+]+)/g;
var match = regex.exec(ua);
var browser = { };
while (match != null) {
var prop = {};
if (match[1]) {
prop.type = match[1];
prop.version = match[2];
}
else if (match[3]) {
prop.type = match[3];
prop.version = match[4];
}
else {
prop.type = match[5];
}
// some expressions have leading whitespace (i couldn't avoid this without a more complex expression)
// trim them and get rid of '.' (' .NET CLR' = 'net_clr')
prop.type = $.trim(prop.type).replace(".","").replace(" ","_");
var value = prop.version ? prop.version : true;
if (browser[prop.type]) {
if (!$.isArray(browser[prop.type]))
browser[prop.type] = new Array(browser[prop.type]);
browser[prop.type].push(value);
}
else browser[prop.type] = value;
match = regex.exec(ua);
}
for (var i in browser)
if (i.indexOf("mac") > -1)
browser.mac = true;
if (browser.windows_nt && !browser.windows)
browser.windows = true;
//put known browsers into the 'version' property for 'some' jquery compatibility
//for sake of argument chromium 'is' chrome
if (browser.chromium && !browser.chrome)
browser.chrome = browser.chromium;
//chrome / safari / webkit
if (browser.chrome) {
browser.version = browser.chrome;
}
else if (browser.safari) {
browser.version = browser.safari;
}
else {
if (browser.applewebkit)
browser.webkit = browser.applewebkit;
if (browser.webkit)
browser.version = browser.webkit;
}
//firefox / gecko
if (browser.firefox) {
if (browser.rv)
browser.version = browser.rv;
else browser.version = browser.firefox;
}
else if (browser.gecko) {
if (browser.rv)
browser.version = browser.rv;
else browser.version = browser.gecko;
}
//opera
if (browser.opera && !browser.version)
browser.version = browser.opera;
//msie
if (browser.trident && browser.rv) //ie11
browser.msie = browser.rv;
if (browser.msie)
browser.version = browser.msie;
$.browser = browser;//Rename to reduce confliction?
//WAS USED FOR TESTING & DISCOVERY (very useful)
//TODO: remove line below
alert(JSON.stringify($.browser));
}) (jQuery);
On Internet Explorer 10, JSON.stringify will ouput something like this:
{"mozilla":"5.0","msie":"10.0","windows_nt":"6.2","trident":"6.0","net4.0e":true,"net4.0c":true,"net_clr":["3.5.30729","2.0.50727","3.0.30729"],"windows":true,"version":"10.0"}