Browser detection with Rails?

1,452 views
Skip to first unread message

Andi Geheim

unread,
Aug 7, 2008, 8:57:03 AM8/7/08
to rubyonra...@googlegroups.com
hi

I can't find any posibility to detect the browser with rails so i tried
javascript.

-----------------------------------------------------------------------------
<SCRIPT Language="JavaScript">
if (navigator.userAgent.indexOf("Netscape") != -1)
{
<% flash[:error_messages_for] = "NETSCAPE" %>
}
else if (navigator.appName.indexOf("Internet Explorer") != -1)
{
<% flash[:error_messages_for] = "IE>" %>
}
</script>
-----------------------------------------------------------------------------

The problem is, that the flash message will shown independent of the IF
request.

Any ideas?

Greeting Chaos
--
Posted via http://www.ruby-forum.com/.

Shandy Nantz

unread,
Aug 7, 2008, 11:09:31 AM8/7/08
to rubyonra...@googlegroups.com
Andi Geheim wrote:
> hi
>
> I can't find any posibility to detect the browser with rails so i tried
> javascript.
>
> Any ideas?
>
> Greeting Chaos

Did you try using request.env['HTTP_REFERER']? I think this can be used
to detect the browser type. Not sure what all the info that this
returned but I think browser type was one of them.

-S

Hubert Łępicki

unread,
Aug 7, 2008, 11:32:51 AM8/7/08
to Ruby on Rails: Talk
I once used something like this:

def client_browser_name
user_agent =
request.env['HTTP_USER_AGENT'].downcase
if user_agent =~ /msie/i
"Internet Explorer"
elsif user_agent =~ /konqueror/i
"Konqueror"
elsif user_agent =~ /gecko/i
"Mozilla"
elsif user_agent =~ /opera/i
"Opera"
elsif user_agent =~ /applewebkit/i
"Safari"
else
"Unknown"
end
end

On Aug 7, 5:09 pm, Shandy Nantz <rails-mailing-l...@andreas-s.net>
wrote:

Andi Geheim

unread,
Aug 7, 2008, 2:24:47 PM8/7/08
to rubyonra...@googlegroups.com
Thanks a lot to both! The solution works great!

cya chaos

Walter Lockhart

unread,
Aug 8, 2008, 4:30:14 AM8/8/08
to Ruby on Rails: Talk
Hi Andi,

where in your Rails application did you place this method? And from
where did you call this method? In other words, how did you use this
code?

Thanks in advance.

Walter

On Aug 7, 7:24 pm, Andi Geheim <rails-mailing-l...@andreas-s.net>
wrote:

Andi Geheim

unread,
Aug 8, 2008, 4:55:59 AM8/8/08
to rubyonra...@googlegroups.com
Walter Lockhart wrote:
> Hi Andi,
>
> where in your Rails application did you place this method? And from
> where did you call this method? In other words, how did you use this
> code?
>
> Thanks in advance.
>
> Walter
>
> On Aug 7, 7:24�pm, Andi Geheim <rails-mailing-l...@andreas-s.net>

Hi Walter,

I placed the method in the application_helper.rb in the app/helpers
directory.

Then I call the method in the application layer in
app/views/layer/application.rb with <% client_browser_name %>. But you
can call this method also in every other view.

Ask if you have still questions.

Greetings Andi

Kris Ventiso

unread,
Aug 30, 2008, 11:40:19 PM8/30/08
to rubyonra...@googlegroups.com
Hi,

I am using this code based on the javascript browser detection code in
the open source ExtJS framework (ExtJS.com) and converted to Rails:

def getBrowser(bt)
rs=false
ua=request.env['HTTP_USER_AGENT'].downcase
isOpera = ua.index('opera') ? true : false
isSafari = (ua =~ /webkit|khtml/) ? true : false
isSafari3 = (ua.index('webkit/5') ? true : false
isGecko = (!isSafari and ua.index('gecko')) ? true : false
isGecko3 = (!isSafari and ua.index('rv:1.9')) ? true : false
isIE = (!isOpera and ua.index('msie')) ? true : false
isIE7 = (!isOpera and ua.index('msie 7')) ? true : false
case bt
when 0 #isKonqueror
if ua.index('konqueror') then rs=true end
when 1 #isOpera
rs=isOpera
when 2 #isSafari
rs=isSafari
when 3 #isSafari2
rs=isSafari && !isSafari3
when 4 #isSafari3
rs=isSafari3
when 5 #isIE
rs=isIE
when 6 #isIE6
rs=isIE && !isIE7
when 7 #isIE7
rs=isIE7
when 8 #isGecko
rs=isGecko
when 9 #isGecko2
rs=isGecko && !isGecko3
when 10 #isGecko3
rs=isGecko3
when 11 #isWindows
if ua.index('windows') or ua.index('win32') then rs=true end
when 12 #isMac
if ua.index('macintosh') or ua.index('mac os x') then rs=true
end
when 13 #isAir
if ua.index('adobeair') then rs=true end
when 14 #isLinux
if ua.index('linux') then rs=true end
when 15 #isSecure
s = request.env['SERVER_PROTOCOL'].downcase
if s.index('https') then rs=true end
end
rs
end

Ben Allfree

unread,
Mar 27, 2009, 9:28:49 AM3/27/09
to rubyonra...@googlegroups.com
One typo above:

isSafari3 = (ua.index('webkit/5')) ? true : false

Reply all
Reply to author
Forward
0 new messages