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 i
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
browsers_detection_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
Try the browser gem. Very simple and can solve your purpose.
You can find the gem Here very easy to use.
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
Though this is a frontend solution, I prefer to check for a specific feature that is required, such as flexbox. You can use something like Modernizr, or just a simple JavaScript check will do (https://stackoverflow.com/a/27047981/179311).
if(!("flexWrap" in document.documentElement.style)) {
// IE 10, Chrome 20 and Firefox 27 and anything older will hit this.
alert('your browser is out of date');
// maybe this is preferred
// window.location = '/browser-unsupported'
}
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.