I test all my jQuery plugin releases in a simple testing environment that I developed. It’s nothing sophisticated, but it allows me to do much of the testing automatically and generates nice reports. The results of the tests are saved in a database, and for each test there is (among other stuff) information about which plugin version was tested, with which jQuery version, and on what browser.
For that purpose, I wanted to extract the browser name and version number from the user agent string that browsers use to reveal their identity to web pages. However, it’s not that simple, since there is no common format of the user agent string and each browser vendor seems to have their own idea of what to put in it.
For example — which version of Opera is this?
Opera/9.80 (X11; Linux i686; U; en) Presto/2.2.15 Version/10.10
Is it 9.80, or 10.10? If you’re curious, it’s 10.10, and this
Dev.Opera blog post explains it.
(If you’re even more curious about why this user agent string business is such a mess, I recommend you read this story — apart from being informative, it’s also funny.)
Anyway, for my testing platform I wrote a JavaScript function that extracts the browser name and version (by default just the major and minor release number) from the user agent string. I’m posting it here in case someone finds it useful too. It recognizes the “big five” browsers that have a significant market share (Firefox, Internet Explorer, Opera, Chrome, and Safari), as these are the browsers that I test my plugins on.
Here’s the source code:
/**
* Extracts the browser name and version number from user agent string.
*
* @param userAgent
* The user agent string to parse. If not specified, the contents of
* navigator.userAgent are parsed.
* @param elements
* How many elements of the version number should be returned. A
* value of 0 means the whole version. If not specified, defaults to
* 2 (major and minor release number).
* @return A string containing the browser name and version number, or null if
* the user agent string is unknown.
*/
function identifyBrowser(userAgent, elements) {
var regexps = {
'Chrome': [ /Chrome\/(\S+)/ ],
'Firefox': [ /Firefox\/(\S+)/ ],
'MSIE': [ /MSIE (\S+);/ ],
'Opera': [
/Opera\/.*?Version\/(\S+)/, /* Opera 10 */
/Opera\/(\S+)/ /* Opera 9 and older */
],
'Safari': [ /Version\/(\S+).*?Safari\// ]
},
re, m, browser, version;
if (userAgent === undefined)
userAgent = navigator.userAgent;
if (elements === undefined)
elements = 2;
else if (elements === 0)
elements = 1337;
for (browser in regexps)
while (re = regexps[browser].shift())
if (m = userAgent.match(re)) {
version = (m[1].match(new RegExp('[^.]+(?:\.[^.]+){0,' + --elements + '}')))[0];
return browser + ' ' + version;
}
return null;
}
A few examples of user agent strings and the returned results:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.9) Gecko/20100825 Ubuntu/9.10 (karmic) Firefox/3.6.9
Firefox 3.6
Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.19 Safari/533.4
Chrome 5.0
Opera/9.80 (X11; Linux i686; U; en) Presto/2.2.15 Version/10.10
Opera 10.10
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
MSIE 8.0