Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect browser type and its version

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) 
like image 976
Syed Raza Avatar asked Apr 26 '11 08:04

Syed Raza


People also ask

What is my browser type and version?

In the browser's toolbar, click on “Help"or the Settings icon. Click the menu option that begins “About” and you'll see what type and version of browser you are using.

How do I identify my browser?

In the browser window, hold the Alt key and press H to bring up the Help menu. Click About Google Chrome and locate the version at the top of the window that appears.

How can you find out the version information of the client's browser?

To detect the browser version on the client machine, your script can analyze the value of navigator. appVersion or navigator. userAgent.

How do I know if I am using IE or Chrome in JavaScript?

let chromeAgent = userAgentString. indexOf( "Chrome" ) > -1; Detecting the Internet Explorer browser: The user-agent of the Internet Explorer browser is “MSIE” or “rv:”.


2 Answers

Try the browser gem. Very simple and can solve your purpose.

You can find the gem Here very easy to use.

like image 132
Nishutosh Sharma Avatar answered Sep 28 '22 11:09

Nishutosh Sharma


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 
like image 29
TH Afridi Avatar answered Sep 28 '22 13:09

TH Afridi