Share via


What Will Windows Internet Explorer Report as the User-Agent String?

The user-agent string is a browser’s identity as reported to Websites via HTTP traffic. A Web developer can use the user-agent string to detect the browser a viewer is using to view a Web site. Understanding the best practices for browser detection ensures that your site continues to operate as intended when viewed by Windows Internet Explorer 8 clients.

This document will explain the structure of the user-agent string, and the values Windows Internet Explorer reports to the server. Also, some example code will be given to assist Web developers with implementing and maintaining recommended browser detection practices.

Understanding the User-Agent String

When you request a Web page, your browser sends a number of headers to the Web server that is hosting the site you're visiting. Each header contains details that help the Web server determine the best way to provide the information you've requested. One header, called the user-agent, identifies the application requesting the information from the server. The user-agent string can contain optional details, called tokens, which must be surrounded by parentheses and that vary among programs. Windows Internet Explorer uses tokens to describe additional details about your computer system.

For historical reasons, Windows Internet Explorer identifies itself as a Mozilla 4.0 browser.

The user-agent string sample above contains three tokens:

  1. The (shown here as "compatible"), which is used by most modern browsers. It indicates that Windows Internet Explorer is compatible with a common set of features.
  2. The , which identifies the browser and contains the version number. The version token in Figure 1 ("MSIE 8.0") identifies Windows Internet Explorer 8. Windows Internet Explorer 7 would return the version token “MSIE 7.0”.
  3. The , which identifies the operating system and contains the version number. The platform token in Figure 1 ("Windows NT 6.0") indicates Windows Vista.

In Figure 1, Windows Internet Explorer 8 is the user-agent. However, other programs also provide user-agent strings when contacting servers over the Internet.

You can easily determine what your browser sends as its user-agent string by typing the following line of code into the address bar.

javascript:alert(navigator.userAgent)

When the code runs, it will display a pop-up alert message with your browser’s entire user-agent string.

For more information regarding user-agent strings, see Understanding User-Agent Strings.

Browser Detection Using the User-Agent String

You can use client-side scripting to parse the user-agent string and extract the version number from the version token to detect Windows Internet Explorer.

The following code example shows two Microsoft Jscript functions that together represent the best technique for determining which version of Windows Internet Explorer a viewer is using.

function getInternetExplorerVersion()
// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
{
   var rv = -1; // Return value assumes failure.
   if (navigator.appName == 'Microsoft Internet Explorer')
   {
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
         rv = parseFloat( RegExp.$1 );
   }
   return rv;
}
function checkVersion()
{
   var msg = "You're not using Windows Internet Explorer.";
   var ver = getInternetExplorerVersion();
   if ( ver> -1 )
   {
      if ( ver>= 8.0 )
         msg = "You're using a recent copy of Windows Internet Explorer."
      else
         msg = "You should upgrade your copy of Windows Internet Explorer.";
    }
   alert( msg );
}

In this example, the function getInternetExplorerVersion() parses the user-agent string sent by the browser and returns the version number. The function checkVersion() calls getInternetExplorerVersion() and compares the returned value to the version number that you wish to check for, in this example, Version 8. You should notice that a greater-than-or-equal comparison is used. This ensures that the function is ready for future versions of Windows Internet Explorer and that you won’t have to update the code if a new version of Windows Internet Explorer is released.

Other Browser Detection Techniques

A version vector refers to Windows Internet Explorer’s internal version number, which is stored in a registry key read on a browser start-up. Conditional comments can be used with version vectors to detect the browser version. You can also use the version vector instead of, or in addition to, user-agent strings to detect the browser being used to view the Web site. For more information about version vectors and how to use them to detect browser versions, read Version Vectors.

An advantage of using client-side scripting is that browsers other than Windows Internet Explorer can be detected. Scripting is also more flexible than conditional comments. Conditional comments are effective if specifically targeting Windows Internet Explorer, such as for custom CSS settings.