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.
on 2006-04-11 00:05
on 2006-04-11 00:28
on 2006-04-11 02:48
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--t142... Sent from the RubyOnRails Users forum at Nabble.com.
on 2006-04-11 03:30
+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
on 2006-04-14 08:45
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.
on 2006-04-15 10:41
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 :) -- olegf
on 2006-04-15 19:22
I also create my own IE specific stylesheet. I include the styleshee in all browsers but shield it with: <!--[if lt IE 7.]> <style type="text/css"> @import url("/stylesheets/iefix.css"); </style> <![endif]--> 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