Monday 24 January 2011

ASP & jQuery: Obtain information about the browser (Browser Capabilities Component)

   


There is a quite obscure asp component which might become useful in some - I believe exceptional - situations. The Browser Capabilities Component creates a BrowserType object which can be used to determine almost everything about a visitor's browser.
How does it work?
The component works quite easily. Every time a page is visited, the browser sends to the server a User Agent Header containing information about the browser itself. Those information are then compared by the BrowserType object to an ini file called "browscap.ini" that sits on the server. When there is a match, the BrowserType object stores different information about the visitor's browser.

Browscap.ini
The misterious ini file is located in %SystemRoot%\system32\inetsrv. In the mentioned dir, on your server, you will find the "browscap.ini" and the "browscap.dll". And here the story begins... In fact you might wonder how or when (or even why) the ini file is updated. Well, the answer is quite disarming: never. Considering the great amount of browser version changes and considering how many new browsers have been developed lately, it is quite sure that the browscap.ini on your server is outdated. If so, what is the real value of the BrowserType object? Consider that when the Browser Capabilities Component can't match the User Agent Header with information in the ini file, the BrowserType object will set all properties to a distressing "UNKNOWN".
Said that, it is very important to keep the "Browscap.ini" file updated. To do so you can visit the Browser Capabilities Project web site, where you can download an updated version of the file (hurray!).

How to use the object
The use of the BrowserType object is very simple.
First of all, you have to create the object:
<%
Set bt = Server.CreateObject("MSWC.BrowserType")
%>
and then you can browse (no pun intended!) all the properties:
<table border="0" width="100%">
<tr>
<th>Client OS</th><th><%=bt.platform%></th>
</tr><tr>
<td >Web Browser</td><td ><%=bt.browser%></td>
</tr><tr>
<td>Browser version</td><td><%=bt.version%></td>
</tr><tr>
<td>Frame support?</td><td><%=bt.frames%></td>
</tr><tr>
<td>Table support?</td><td><%=bt.tables%></td>
</tr><tr>
<td>Sound support?</td><td><%=bt.backgroundsounds%></td>
</tr><tr>
<td>Cookies support?</td><td><%=bt.cookies%></td>
</tr><tr>
<td>VBScript support?</td><td><%=bt.vbscript%></td>
</tr><tr>
<td>JavaScript support?</td><td><%=bt.javascript%></td>
</tr>
</table>
And that's it!

Weren't you saying jQuery?
Oh, yes! You're right, I did write jQuery! That is because in the jquery library you can find one property of the global jQuery object that is called jQuery.browser.
jQuery.browser can be used to detect the web browser type. It has 5 different flags:
  • webkit
  • safari (deprecated)
  • opera
  • msie
  • mozilla
and the usage is:
$.browser.webkit
$.browser.safari
$.browser.opera
$.browser.msie
$.browser.mozilla
The property will return "true" when there's a match between the flag and the web browser type (remember the header?).
You can then use the property as in the following example:
if ( ($.browser.msie) ){
alert('IE 6')
}
Finally I must let you know that jQuery official api reference states, “The $.browser property is deprecated in jQuery 1.3, but there are no immediate plans to remove it.”

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.