Get Browser name from User agent String

Earlier this week, I was working on a task requiring me to get  the browser  name visitors used to browse the website.A quick Google search gave me  hundreds of results most of which referring to ASP.Net’s request Object .This request object  contains  a property  browser  that returns an object of type HttpBrowserCapabilities.This object contains properties  such as browser, version,major version,and minor version that you can use to determine the client browser type .

I go YAY!!! .That’s exactly what I was looking for  .I then jumped  into coding to  implement it ,only to be broken when my manager tested it .The problem is it works fine for IE,Firefox but returns browser type as Desktop for Chrome .

Browse the following url in chrome and you will know what I mean

http://www.4guysfromrolla.com/demos/BrowserDetection.aspx

If you have come across similar scenario, then use the code snippet below which uses browser’s user agent property to correctly return browser type

Public Shared Function GetBrowserName() As String
        Dim userAgent As String = Request.UserAgent
        If userAgent.Contains("MSIE 5.0") Then
            Return "Internet Explorer 5.0"
        ElseIf userAgent.Contains("MSIE 6.0") Then
            Return "Internet Explorer 6.0"
        ElseIf userAgent.Contains("MSIE 7.0") Then
            Return "Internet Explorer 7.0"
        ElseIf userAgent.Contains("MSIE 8.0") Then
            Return "Internet Explorer 8.0"
        ElseIf userAgent.Contains("MSIE 9.0") Then
            Return "Internet Explorer 9.0"
        ElseIf userAgent.Contains("Firefox") Then
            Return userAgent.Substring(userAgent.IndexOf("Firefox")).Replace("/", " ")
        ElseIf userAgent.Contains("Opera") Then
            Return userAgent.Substring(userAgent.IndexOf("Opera"))
        ElseIf userAgent.Contains("Chrome") Then
            Dim agentPart As String = userAgent.Substring(userAgent.IndexOf("Chrome"))
            Return agentPart.Substring(0, agentPart.IndexOf("Safari") – 1).Replace("/", " ")
        ElseIf userAgent.Contains("Safari") Then
            Dim agentPart As String = userAgent.Substring(userAgent.IndexOf("Version"))
            Dim version As String = agentPart.Substring(0, agentPart.IndexOf("Safari") – 1).Replace("Version/", "")
            Return "Safari " & version
        ElseIf userAgent.Contains("Konqueror") Then
            Dim agentPart As String = userAgent.Substring(userAgent.IndexOf("Konqueror"))
            Return agentPart.Substring(0, agentPart.IndexOf(";")).Replace("/", " ")
        ElseIf userAgent.ToLower.Contains("bot") Or userAgent.ToLower.Contains("search") Then
            Return "Search Bot"
        End If
        Return "Unknown Browser or Bot"
    End Function
End Class

 

Hope this saves you guys some time and effort .As always your feedback and criticisms are always welcome .

Happy coding

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s