Tuesday, April 07, 2015

Browser Detection

Using the user agent to detect the browser type and version - I personally don't like this method, though used in majority of websites. Any new version of browser released to market, need to identify the uniqueness in the user agent that helps in identifying the browser. Additionally, MS IE user agent string is not unique when it works in compatibility and non-compatibility mode, making it much more complicated than other browsers.

 A better version is to make use of IIS component - MSWC and browscap.ini file. Internally, even this logic also make use of the user agent. However, these are tested, release candidates and hides all the internals from developers. Below are the steps to make use of this component in classic asp.
(Note that for other technologies, go to http://browscap.org/ website and download the xml or any other appropriate version of browscap file.)

  1. Get the latest version of browscap.ini file (ASP Version). Note that any new browser added to the market, this step should be repeated OR you can edit already downloaded file.
  2. Rename the file to browscap.ini and place the file in C:\Windows\System32\inetsrv folder. If 64 bit machine, then place it under C:\Windows\SysWOW64\inetsrv folder.
  3. Save the below code as asp file
<%
Set browser = Server.CreateObject("MSWC.BrowserType")

' some browser properties that can be accessed via BrowserType object. There are more - google.
browser=browserdetect.Browser
version=browserdetect.Version

' print some output
response.write ("browser: " & browser & "
")
response.write ("version: " & version & "
")
%>

That's it - access the asp file and you should see the output displaying browser type, version and other details. Isn't that simple and the code looks much simpler and neat.

No comments: