Browser Detection

For some features, you will be able to use a single implementation that will either work in all browsers, or degrade gracefully in some. For other functionality, however, you may want to check the version of the client browser so you can provide an alternate implementation if the browser does not support the specific features you would like to use.

There are several techniques that can be used to determine the browser vendor and version: 

Client Side

You can check the browser version on the client side (meaning the script is executed on the client side only and no information exchange takes place with the server) by using the navigator.userAgent object. An easy way to identify whether the browser is Internet Explorer and determine its version number is to use the following JScript function:

function msieversion()
// return Microsoft Internet Explorer (major)
// version number, or 0 for others.
// This function works by finding the "MSIE "
// string and extracting the version number
// following the space, up to the decimal point
// for the minor version, which is ignored.
var ua = window.navigator.userAgent
var msie = ua.indexOf ( "MSIE " )
if ( msie > 0 )      // is Microsoft Internet Explorer; return version number
   return parseInt ( ua.substring ( msie+5, ua.indexOf ( ".", msie ) ) )
   return 0          // is other browser

This function runs on the large majority of browsers and returns the major version number for any Microsoft Internet Explorer browser, or zero for all other browsers. Use of this function assures that the script will be compatible with future versions of Internet Explorer.

Scripts should always check version numbers "greater than or equal to" rather than just "equal to" to be compatible with future versions of the browser. Existing scripts that check for userAgent equal to "MSIE 3" should be changed to check the version correctly so that these scripts will recognize Internet Explorer 4.0.

The following example shows how to correctly check the client browser version:

if ( msieversion() >= 4 ) {
   [code for Internet Explorer 4.0 browsers]
else if ( msieversion() <= 3 ) {
   [code for other IE browsers]
else {
   [code for other browsers]

Server Side

You can also detect the browser on the server side, using Active Server Pages (ASP), CGI, or some other server-side solution, and send the client entirely different content based on the browser version. The following example demonstrates this technique using ASP:

set bc = Server.CreateObject("MSWC.BrowserType")

if bc.browser="IE" then
   if bc.majorver="4" then
      [code for Internet Explorer 4.0 browsers]
      [code for other Internet Explorer browsers]
   [code for other browsers]
end if

For this solution, your pages will need to be hosted on a server that supports ASP.

browser detection javascript asp userAgent html

Back To Top
© 1998 - 2024
Version 7.21 | Advertise on this site