Detecting browser type?


#1

I’m wondering how i can detect the browser type for the client. I know
this
is possible, but i cant seem to find how to do this, nor any example
code
for this.

I would appreciate if someone could point me to some info or just give
me an
explanation.

thanks in advance.


#2

Manish S. wrote:

I’m wondering how i can detect the browser type for the client. I know
this
is possible, but i cant seem to find how to do this, nor any example
code
for this.

I would appreciate if someone could point me to some info or just give
me an
explanation.

thanks in advance.

You should think very carefully about whether or not you want to do
this. Remember, users can change their user agent strings, and it’s a
foolish idea to rely on them. It’s an evil idea to try to disable things
based on those user agent strings, because you’ll invariably end up ‘out
of date’ with respect to browsers’ features, and with irritated users
having to change their user agent strings to use your web app.

If you really need to do things differently for different browsers
(other than in CSS), you should detect browser features in
javascript…

if(window.XMLHttpRequest){
request = new XMLHttpRequest();
request.onreadystatechange=handleResponse;
request.open(“GET”,theURL,true);
request.send(null);
}


#3

But… if you must, here’s an example how. This just checks for IE.
You’ll
want to parse the whole user-agent string. A reasonable reference on
user
agent strings is http://en.wikipedia.org/wiki/User_agent.

<%= stylesheet_link_tag(‘ie’, :media => ‘all’) if
request.env[‘HTTP_USER_AGENT’] =~ /MSIE/ %>


View this message in context:
http://www.nabble.com/detecting-browser-type--t1427349.html#a3851912
Sent from the RubyOnRails Users forum at Nabble.com.


#4

+1 for testing by browser object support =>
http://www.google.co.uk/search?q=browser+sniff+object

-10 for testing by ua

ua strings are unreliable at best, don’t do it


#5

thanks for the suggestions…i was actually using it to collect stats
about
browsers most used. Its not that important and i know it can faked, but
overall should give a good idea of most common browser used.


#6

Recently, I started to like the idea of separate stylesheet for IE.
Usually, I have three stylesheets - one for standard compliant
browsers (more or less) - firefox, last revs of opera, safari and any
other with gecko or khtml engine. The rendering differences are very
minor for these browsers and it is easy to have clean css. The second
stylesheet is for IE. I just take my standard-complaint (no hacks)
stylesheet and modify the parts that are not working in IE. You may
want to not to have a third stylesheet and serve one of the
beforementioned stylesheets to the unknown browsers. But I do have
third one. It uses very basic css and should work in old operas and
other 1% market share browsers. At least, these users can access your
content now.

Then I do detect browser by user-agent. Yes, we’ve heard that it is
not good, for user can change his user-agent string. But:

  1. Most(really) of them don’t (especially those IT-ignorant)
  2. If they do, then they must whay they do or face consequences

This tehnique works for me. Haven’t got any complaints.

And I don’t need much more time to maintain 3 separate stylesheets for
it is easy to create standard-compliant version (no hacks). And then
you just need to tweak it for IE without worry that your hacks will
break the intended behaviour of all good browsers. Third stylesheet is
really simple - only basic styling to get accessible content.

My approach is inspired by
http://www.stylegala.com/articles/no_more_css_hacks.htm
I dislike the idea to mix code and css, so I invented my own approach.

Just my .02 cents :slight_smile:


olegf


#7

I also create my own IE specific stylesheet. I include the styleshee in
all
browsers but shield it with:

I also aggregate all my CSS into one file, so I have helper methods that
look in /stylesheets/iefix/ for files and aggregates those into
iefix.css