Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser Detection

What's the best / simplest / most accurate way to detect the browser of a user?

Ease of extendability and implementation is a plus.

The less technologies used, the better.

The solution can be server side, client side, or both. The results should eventually end up at the server, though.

The solution can be framework agnostic.

The solution will only be used for reporting purposes.

like image 978
Jrgns Avatar asked Sep 19 '08 10:09

Jrgns


People also ask

What is browser detection used for?

Determining the type and version of Web browser that made a request. It is used to deliver a different Web page based on the browser's capabilities.

What is browser identification?

“A device fingerprint, machine fingerprint, or browser fingerprint is information collected about a remote computing device for the purpose of identification. Fingerprints can be used to fully or partially identify individual users or devices even when cookies are turned off.”

What is a browser on your phone?

A mobile browser is a web browser built to use on mobile devices like mobile phones or personal digital assistants (PDAs). Mobile browsers are designed in such a way that it can display Web content most efficiently for small screens used on mobile devices.


2 Answers

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?

like image 103
Dan Avatar answered Nov 10 '22 09:11

Dan


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.

like image 37
ConroyP Avatar answered Nov 10 '22 07:11

ConroyP