How to detect browser type and its version

后端 未结 5 1988
借酒劲吻你
借酒劲吻你 2021-02-03 21:54

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

5条回答
  •  广开言路
    2021-02-03 22:28

    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
    

提交回复
热议问题