# Detecting browser type?

**URL:** <https://www.ruby-forum.com/t/detecting-browser-type/57427>\
**Category:** Rails\
**Created:** [April 10, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/detecting-browser-type/57427 "2006-04-10T22:05:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [April 10, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/detecting-browser-type/57427/1 "2006-04-10T22:05:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [April 10, 2006, 10:28pm UTC](https://www.ruby-forum.com/t/detecting-browser-type/57427/2 "2006-04-10T22:28:05Z")

</div>

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);  
}

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [April 11, 2006, 12:48am UTC](https://www.ruby-forum.com/t/detecting-browser-type/57427/3 "2006-04-11T00:48:27Z")

</div>

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 [User agent - Wikipedia](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](http://www.nabble.com/detecting-browser-type--t1427349.html#a3851912)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [April 11, 2006, 1:30am UTC](https://www.ruby-forum.com/t/detecting-browser-type/57427/4 "2006-04-11T01:30:57Z")

</div>

+1 for testing by browser object support =\>  
[http://www.google.co.uk/search?q=browser+sniff+object](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

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [April 14, 2006, 6:45am UTC](https://www.ruby-forum.com/t/detecting-browser-type/57427/5 "2006-04-14T06:45:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [April 15, 2006, 8:41am UTC](https://www.ruby-forum.com/t/detecting-browser-type/57427/6 "2006-04-15T08:41:23Z")

</div>

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](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 🙂

–  
olegf

---

<div class="post-metadata">

**Author:** ![mshah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mshah](https://www.ruby-forum.com/u/mshah)\
**Post date:** [April 15, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/detecting-browser-type/57427/7 "2006-04-15T17:22:32Z")

</div>

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
