Browser detection
On the server you're pretty much limited to the UserAgent string the browser provides (which is fraught with problems, have a read about the UserAgent string's history).
On the client (ie in Javascript), you have more options. But the best option is to not actually worry about working out which browser it is. Simply check to make sure whatever feature you want to use actually exists.
For example, you might want to use setCapture, which only MSIE provides:
if (element.setCapture) element.setCapture()
Rather than working out what the browser is, and then inferring its capabilities, we're simply seeing if it supports something before using it - who knows what browsers will support what in the future, do you really want to have to go back and update your scripts if Safari decides to support setCapture?
When using javascript: Don't use browser detection
Write code that tests itself for given cases exhibited by browsers, otherwise you'll simply be writing code for a very very small population. Its better to use "typeof foo == 'undefined'"
and browser specific tricks where you need them.
jQuery does this all over its codebase ( if you look at the code you'll see it implementing behaviours for different browser tecnologies )
Its better in the long run.
The JQuery Browser Plugin will do it client-side for you.
What is the jQuery Browser Plugin?
The jQuery Browser Plugin is an addon for jQuery that makes it easy to uniquely identify your visitors' browsers.
What does it do?
It gives you an object in javascript that contains all of the information about the browser being used. It also adds CSS browser selectors, which means you can style elements or write functions for specific browsers, browser versions, layouts, layout versions, and even operating systems. Image of the jQuery Browser Plugin in action.
The plug-in makes $.browser
available, which you can re-submit to your server via an AJAX call, if you really need it server-side.
alert($.browser.name); // Alerts Firefox for me
The plug-in will only be as effective as the browsers it's been tested against, however. The plugin listed above has a list of browsers recognised in it's tests, but there's always the risk that a new browser will come sneaking out (Google Chrome..) that will require a re-write of the recognition rules. That said, this plug-in seems to be regularly updated.