How to detect browser type and its version
Solution 1
Try the browser gem. Very simple and can solve your purpose.
You can find the gem Here very easy to use.
Solution 2
NOTE: Be careful some search engines see this as a type of intrusion See ( Google HTTP USER AGENT )
if request.env['HTTP_USER_AGENT'] =~ /[^\(]*[^\)]Chrome\//
or in the case of firefox
if request.env['HTTP_USER_AGENT'] =~ /[^\(]*[^\)]*[^\t]Firefox\//
and check this here you will get all you need browser Gem
and here is a method which can detect all browsers so chill man
def browser_detection
result = request.env['HTTP_USER_AGENT']
browser_compatible = ''
if result =~ /Safari/
unless result =~ /Chrome/
version = result.split('Version/')[1].split(' ').first.split('.').first
browser_compatible = 'Application is not functional for Safari version\'s '+version if version.to_i < 5
else
version = result.split('Chrome/')[1].split(' ').first.split('.').first
browser_compatible = 'Application is not functional for Chrome version\'s '+version if version.to_i < 10
end
elsif result =~ /Firefox/
version = result.split('Firefox/')[1].split('.').first
browser_compatible = 'Application is not functional for Firefox version\'s '+version if version.to_i < 5
elsif result =~ /Opera/
version = result.split('Version/')[1].split('.').first
browser_compatible = 'Application is not functional for Opera version\'s '+version if version.to_i < 11
elsif result =~ /MSIE/
version = result.split('MSIE')[1].split(' ').first
browser_compatible = 'Application is not functional for Microsoft Internet Explorer version\'s '+version if version.to_i < 9
end
browser_compatible
end
Solution 3
There is also useragent gem:
<% user_agent = UserAgent.parse(request.env["HTTP_USER_AGENT"]) %>
App: <%= user_agent.application %> # Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:22.0)
Browser: <%= user_agent.browser %> # Firefox
Version: <%= user_agent.version %> # 22.0
Platform: <%= user_agent.platform %> # Macintosh
Mobile: <%= user_agent.mobile? %> # False
OS: <%= user_agent.os %> # OS X 10.8
Solution 4
What you do is actually the way to do it. Now, you can process the user agent information with a regular expression, looking for matches on Firefox, Chrome or any other browser or version you like.
Syed Raza
Sr Principal Software Engineer(October 2013 – Present) Ebryx, Lahore Pakistan Mobile Technologies PhoneGap with (AngularJS / HTML5 / CSS3 / JQuery/ Twitter Bootstrap) JavaScript Technologies AngularJS Framework JQuery PHP Technologies Yii Framework Database SQL SERVER 2008 SQL SERVER 2008 Analysis Services (Pivot Reports, DImensions, Facts/ Measures) Webserver Apache IIS node.js Other tools Autodesk Mapserver (Google maps) Mobile Application Backend Services Sr Software Engineer(May 2012 – September 2013) IQVIS, Lahore Pakistan Ruby on Rails 3.2, JQuery, LESS/SASS, CoffeeScript, HAML. Bootstrap (Front End) Java Spring (Backend) MongoDB (Database) J2EE, Spring MVC, Tiles, Hibernate, JSTL HTML5, CSS3, JavaScript and JQuery Ubuntu Linux MYSQL, PostgreSQL, Github and Senior Software Engineer / Team Lead (Jan, 2011 – April, 2012) NextBridge PVT LTD, Lahore Pakistan Web Technology Ruby, Ruby on Rails, JQUERY, HTML 5 and CSS 3 MYSQL, PostgreSQL, Github and Mobile Technology Android SDK (Java) and Phone Gap ( JQuery, Html 5 and ) Work as Freelancer develop Software and Websites ( 2009 – Jan, 2011) PHP5, JQUERY and MYSQL Projects Shopping Carts Real Estate Sites Content Management System C#, ASP.NET and SQL SEVER 2008 Projects Web Form Applications Windows Form Applications Game Programmer (2007) IR Gurus rename to Transmission Games Studio, Melbourne Australia Team Member of Next Generation Sports Title(Cricket Ashes 2009) Game designed ( For PS3, XBOX 360 and Windows PC Platform) Publisher Code masters www.playashes.com In-House Game Engine C++, DirectX, OpenGL
Updated on November 24, 2020Comments
-
Syed Raza over 3 years
how can i detect browser type and its version in Rails. I want to put check on the version of specific browser and if its not required browser version than ask user to upgrade it.. i use below specified command but as its not following a standard pattern am unable to use it.
request.env['HTTP_USER_AGENT'] Chrome out put is below Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16 Safari out put is below Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1 FireFox out put is below Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0 Opera out put is below Opera/9.80 (Windows NT 5.1; U; en) Presto/2.8.131 Version/11.10 Internet Explorer out put is below Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
-
PJP about 13 yearsSniffing the
'HTTP_USER_AGENT'
is the traditional way to do it. It is possible that other apps will spoof your code, or that a user will change their browser's signature to spoof you, so don't trust it explicitly.
-
-
Mosselman over 11 years@SyedRaza Google could have solved that problem for you as well. I mean there is nothing wrong with asking 'how do I go about this and that', but assuming that people will do your work for you here while you already know the 'how' is I think kind of lazy.
-
Mohd Anas almost 8 yearsDo we have any method through which we can detect if the browser version is latest or not? Thanks in advance.