SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Backend Browser Detection : Capabilities Object

The browser capabilities component is a server-side object. To use this component, you must call the Server object's CreateObject method. This method instantiates and returns a reference to an object. To create the browser capabilities component, you need to call CreateObject with the identifier "MSWC.BrowserType".
VBScript

<%
  Set browser = Server.CreateObject("MSWC.BrowserType")
%>

JScript

<%
  var browser = Server.CreateObject("MSWC.BrowserType")
%>

Note: JavaScript on the server is case sensitive just as it is on the client (the same language engine is used). However, the ASP object model does not follow the same capitalization rules as the client-side scripting model (notice the object's and methods are capitalized). If you get scripting errors in JavaScript on what appears to be valid code, be sure to check your capitalization.

For those of you new to ASP scripting, you designate server scripts using special markup: <% ... %>. The contents within this markup are executed on the server and only results of the script, if any, are sent to the client. As we will demonstrate, the scripts contained within the block do not have to be executable by themself. Instead, all the script blocks on your page make up the program so you can end a script to embed HTML and then later continue with the script.

We are going to start by testing the browser version. Most of our pages have three logical paths: IE4, NS4, and other browsers. Therefore, we create 2 variables that represent whether the user has a 4.0 or later browser and store that information in a variable:
VBScript

<%
  ie4 = (browser.version>=4) and (browser.browser="IE")
  ns4 = (browser.version>=4) and (browser.browser="Netscape")
%>

JScript

<%
  ie4 = (browser.version>=4) && (browser.browser=="IE")
  ns4 = (browser.version>=4) && (browser.browser=="Netscape")
%>

Now we have enough information to author browser specific pages. We only provide the JavaScript version but you can easily convert this to VBScript:

<%
  var browser = Server.CreateObject("MSWC.BrowserType")
  ie4 = (browser.version>=4) && (browser.browser=="IE")
  ns4 = (browser.version>=4) && (browser.browser=="Netscape")

  if (ie4) { 
%>
  <P>This is IE4 only content
<%
  } else 
    if (ns4) { 
%>
  <P>This is NS4 only content
<%
  } else {
%>
  <P>This is the downlevel content
<% } %>

Examine the above script closely. Notice how the script's execution ends to allow you to include some HTML, and then later continues. Each script block is executed sequentially in the page. If you were using IE4, the test for IE4 evaluates to true, the contents within the IE4 section are sent to the client, then the script continues executing. Since none of the other conditions are met, the other contents are skipped and not sent to the client.

While in our pages we concentrate only on the browser brand and version, the browser capabilities component exposes additional properties for detecting support for tables, frames, ActiveX, scripting, and more. The set of capaibilities that you can test for are defined by the browser.ini file. This file is a user-customizable and updateable file that associates a set of properties and capabilities with a particular browser version. For more information on customizing the browser.ini file, we recommend you check out the information at Microsoft's web-site.

Next, we continue with a few tips on client versus server-side detection.