User Agent inspection: finding the client browser name

Here is a short snippet of Ruby code that I wrote today to detect client browser types. (to override CSS styles based on client browsers..) Hope it helps.

  def client_browser_name(user_agent_string)
    case user_agent_string
    when /msie 6/i
      "IE6"
    when /msie 7/i
      "IE7"
    when /msie 8/i
      "IE8"
    when /konqueror/i
      "Konqueror"
    when /firefox\/2/i
      "FF2"
    when /firefox\/3/i
      "FF3"
    when /applewebkit/i
      "Safari"
    when /gecko/i       #generic case where FireFox is not in String (ie. MindField, BonEcho, GranParadiso)
      "Mozilla"
    when /opera/i
      "Opera"
    else
      "Unknown"
    end
  end

Leave a Reply

Your email address will not be published. Required fields are marked *