You are currently offline, waiting for your internet to reconnect

How To Determine Browser Version from a Script

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
With the new features that Internet Explorer 4.0 provides, it may bedesirable for both client-side and server-side scripts to determinethe browser version, so that a Web application can either take advantage ofnew functionality, or degrade gracefully, depending on the version of thecurrently-running browser.

IMPORTANT: The methods that are used in this article rely on the Web site receiving the correct "HTTP_USER_AGENT" string for browsers that connect to the site. Some proxy servers and browsers may send information that is incorrect or difficult to interpret. For example, the default HTTP_USER_AGENT string that Opera 5.12 returns is "Mozilla/4.0 (compatible; MSIE 5.0; <operating system>) Opera 5.12 [en]". This string is hard to interpret because the first portion of the string is the same as the string that Internet Explorer 5 returns.

If you need to uniquely identify Internet Explorer version 5 or later (on Microsoft Win32 and Unix platforms only), use the isComponentInstalled or getComponentVersion methods of the CLIENTCAPS behavior. For information about these methods, see the following Microsoft Web sites:NOTE: These methods will fail if the client is not Internet Explorer 5 or later (Microsoft Win32 and Unix platforms only).
More information

From a Client-Side Script

The JScript function below can be used to determine the version of thebrowser it is running on from a client-side script. The function runs onthe large majority of browsers currently available and returns the majorversion number for any Microsoft Internet Explorer browser, and zero (0)for others. Use of this function assures that the script will be compatiblewith future versions of the Internet Explorer.
   // This function returns Internet Explorer's major version number,   // or 0 for others. It works by finding the "MSIE " string and   // extracting the version number following the space, up to the decimal   // point, ignoring the minor version number   <SCRIPT LANGUAGE="JavaSCRIPT">   function msieversion()   {      var ua = window.navigator.userAgent      var msie = ua.indexOf ( "MSIE " )      if ( msie > 0 )      // If Internet Explorer, return version number         return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))      else                 // If another browser, return 0         return 0   }   </SCRIPT>				
When checking version numbers, scripts should always use the >= operator, instead of the = operator to ensure compatibility with future versions. Existing scripts that specifically check for userAgent equal to "MSIE 3," for instance, should be changed to use the >= operator so that the scripts recognize Internet Explorer 4.0 as well.

The following example demonstrates how to detect the browser version from aclient-side script. Note that this example does not specifically check forplatform version, such as Windows 95, Windows NT, Windows 3.1, and so forth, which require a separate userAgent substring check when applicable:
<SCRIPT LANGUAGE="javascript">   if ( msieversion() >= 4 )      document.write ( "This is Internet Explorer 4 or later" );   else if ( msieversion() >= 3 )      document.write ( "This is Internet Explorer 3" );   else      document.write ( "This is another browser" );   </SCRIPT>				

From a Server-Side Script

NOTE: The Request.ServerVariables("HTTP_USER_AGENT") method may be used in place of the BrowserType object in server-side script. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
272413 How To Determine the Browser Type in Server-Side Script Without the BrowserType Object
The Browser Capability component that comes with Active Server Pages (ASP)provides your scripts with a description of the capabilities of theclient's Web browser by comparing the User-Agent HTTP Header with theentries in the BROWSCAP.INI file. In order for an ASP application to detectthe browser's support for the new Internet Explorer 4.0 features, copy thelatest BROWSCAP.INI from the following Cyscape Web site:Copy it to your %windows%\system32\inetsrv\asp\cmpnts directory and executea server-side script similar to the example below.

This example specifically takes advantage of new Dynamic HTML functionalityif the current browser is Internet Explorer 4.0 or later:
<HTML><BODY>   <%  Set bc = Server.CreateObject("MSWC.BrowserType") %>   <table border=0 cellspacing="0" cellpadding="5">      <tr>         <% If bc.browser = "IE" and bc.version >= 4 Then %>         <td><a href="chocolate.htm" TARGET="_TEXT"              onmouseover="'red'"              onmouseout="'purple'">chocolate</a>         </td>         <% Else %>            <td><a href="chocolate.htm" TARGET="_TEXT">chocolate</a></td>         <% End If %>         <td>|</td>         <% If bc.browser = "IE" and bc.version >= 4 Then %>         <td><a href="vanilla.htm" TARGET="_TEXT"              onmouseover="'red'"              onmouseout="'purple'">vanilla</a>         </td>         <% Else %>            <td><a href="vanilla.htm" TARGET="_TEXT">vanilla</a></td>         <% End If %>      </tr>   </table></BODY></HTML>				
Components Reference in the Active Server Pages (ASP) Roadmap
detect verify check

Article ID: 167820 - Last Review: 06/22/2014 18:19:00 - Revision: 3.0

  • kbFAQ kbhowto kbscript KB167820