HOWTO: Determining Browser Version from a Script

Last reviewed: November 10, 1997
Article ID: Q167820
The information in this article applies to:
  • Microsoft Internet Explorer (Programming), versions 3.0, 3.01, 3.02, 4.0

SUMMARY

With the new features that Internet Explorer 4.0 provides, it may be desirable for both client-side and server-side scripts to determine the browser version, so that a web application can either take advantage of new functionality, or degrade gracefully, depending on the version of the currently-running browser.

MORE INFORMATION

From a Client-Side Script

The JScript function below can be used to determine the version of the browser it is running on from a client-side script. The function runs on the large majority of browsers currently available and returns the major version number for any Microsoft Internet Explorer browser, and zero (0) for others. Use of this function assures that the script will be compatible with 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 IE, 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 a client-side script. Note that this example does not specifically check for platform version, such as Windows 95, Windows NT, Windows 3.1, etc., which require a separate userAgent substring check when applicable:

   <SCRIPT LANGUAGE="javascript">
   if ( msieversion() >= 4 )
      document.write ( "This is IE4 or later" );

   else if ( msieversion() >= 3 )
      document.write ( "This is IE3" );

   else
      document.write ( "This is another browser" );
   </SCRIPT>

From a Server-Side Script

The Browser Capability component that comes with Active Server Pages (ASP) provides your scripts with a description of the capabilities of the client’s Web browser by comparing the User-Agent HTTP Header with the entries in the BROWSCAP.INI file. In order for an ASP application to detect the browser's support for the new Internet Explorer 4.0 features, copy the latest BROWSCAP.INI from http://www.microsoft.com/iis/usingiis/developing/updates.htm to your %windows%\system32\inetsrv\asp\cmpnts directory and execute a server-side script similar to the example below.

This example specifically takes advantage of new Dynamic HTML functionality if the current browser is IE 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="this.style.color='red'"
              onmouseout="this.style.color='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="this.style.color='red'"
              onmouseout="this.style.color='purple'">vanilla</a>
         </td>

         <% Else %>
            <td><a href="vanilla.htm" TARGET="_TEXT">vanilla</a></td>
         <% End If %>

      </tr>
   </table>

</BODY> </HTML>

REFERENCES

Components Reference in the Active Server Pages (ASP) Roadmap


Additional query words: detect verify check
Keywords : msiexplore kbhowto
Version : 3.0 3.01 3.02 4.0
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.