Skip to Menu

browser detection

This is a generic way of getting the browser name and version (MSIE, Firefox, Opera, Safari, Netscape) with a regular expression in JavaScript (by executing it against the navigator.userAgent string). Sample code uses jQuery (but only for manipulating the current page, not obtaining the browser details), but would not require much to get it working if you don't use it.

User agent string: Unknown
Browser name: Unknown, Browser version: Unknown

Code

$("#useragent").html(navigator.userAgent);
var reBrowser = /(?:MSIE\s)([\d\.]+)(?!.+Opera)|(?:Firefox|Opera|Safari|Netscape)[\s\/]([\d\.]+)/;
var browser = reBrowser.exec(navigator.userAgent)[0];
if(browser)
{
	$("#browsername").html(browser.match(/[^\s\/]+/)[0]);
	$("#browserversion").html(browser.match(/[\d\.]+/)[0]);
}

Notes

Safari returns the WebKit version and not the Safari version. Even then, the WebKit version can be different even for the same Safari version number (security updates?). I don't have Safari or Mac OSX to do proper tests though.

Opera has a feature to identify itself as Internet Explorer, which is considered in the regular expression. However, Opera 9 allows you to mask it as Internet Explorer (which means it has pretty much the same user agent string as it). The mask does not include any extra information that may be in it though, but which still is part of Internet Explorer's user agent string (like the versions of .NET installed or anything defined in the registry under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\User Agent\Post Platform).