Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
HTML and CSS
 Detecting Internet Explorer More Ef...
Detecting Internet Explorer More Effectively

This topic demonstrates how to detect current and future versions of Windows Internet Explorer more effectively.

This topic contains the following sections.

Introduction

Many Web designers use browser-detection techniques to ensure that their sites display properly on specific browser versions. It is important to ensure that the techniques chosen take into account future browser releases.

This topic shows three ways to detect current and future versions of Internet Explorer. Designers currently detecting browsers in their sites should verify that browser detection is necessary and, if so, ensure that the code accepts later versions of supported browsers.

Several typical browser detection techniques fail when they encounter Internet Explorer 7. For example, some sites that look great when viewed in Microsoft Internet Explorer 6 might display errors when viewed in Internet Explorer 7. This is not caused by any problems with Internet Explorer 7; it happens because the browser detection code is not designed for newer versions of the browser. The examples in this topic are designed to help you design browser detection code that works in current and future versions of Internet Explorer.

Note  It is not recommended that you block access to content based on the user-agent string of the browser. If you do have to offer different content to different versions of the browser due to improved capabilities, you want to ensure that future versions of the browser are not blocked. Serving content based solely on the user-agent string is often an unreliable way to detect the full capabilities of the browser because the user might have adjusted some settings, such as disabling script or extensions.

Parsing the User-Agent String

The most common way to detect Internet Explorer is to use client-side scripting to parse the user-agent string and extract the version number from the version token. The following example shows the preferred way to do this with Microsoft JScript.

function getInternetExplorerVersion()
// Returns the version of 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 Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 6.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  alert( msg );
}

As you review this example, please note the following:

  • A regular expression extracts the version number from the user-agent version token. Regular expressions let you specify optional conditions to match, so that you can match a larger number of version tokens, not just those that match a strict set of conditions.

  • The version number extracted from the version token is formally converted to a numeric value. Care must be taken because pre-release versions of Internet Explorer typically add letters to the version token. For example, the version token for pre-release versions of Internet Explorer 7 is "MSIE 7.0b."

  • The checkVersion() function verifies that the browser is version 6.0 or later; this ensures that the welcome message is displayed for those using newer browsers.

This example properly detects most versions of Internet Explorer, but only if scripting is enabled. The next sections show techniques that work when scripting is disabled.

For more information about the user-agent string and Internet Explorer's user-agent tokens, see Understanding User-Agent Strings.

Using Conditional Comments

If you are specifically interested in Internet Explorer, conditional comments might be a more appropriate choice. The following example shows an effective way to use conditional comments to display custom content.

<!--[if gte IE 6]>
<p>You're using a recent version of Internet Explorer.</p>
<![endif]-->

<!--[if lt IE 6]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<![endif]-->

<![if !IE]>
<p>You're not using Internet Explorer.</p>
<![endif]>

Like the earlier JScript example, this example makes sure the current version is greater than or equal to the specified version number. By doing so, it supports later versions.

In addition, this example carefully mixes downlevel-revealed and downlevel-hidden comments to ensure that each message appears only for the intended browsers.

For more information about conditional comments, see About Conditional Comments.

Using ASP.NET's HttpBrowserCapabilities Object

The previous examples effectively detect Internet Explorer; however, they have their limitations. The JScript example will not work when scripting is disabled, and it forces you to research the version tokens for the Internet Explorer version you want to support.

The conditional comments let you respond to Internet Explorer, but they also limit your opportunities to respond to other browsers.

ASP.NET developers can use the HttpBrowserCapabilities object to detect and respond to almost any browser. In addition, alternate content is not sent to the end-user's browser. The following example shows how to detect Internet Explorer from an ASP.NET application using C#.

private float getInternetExplorerVersion()
{
  // Returns the version of Internet Explorer or a -1
  // (indicating the use of another browser).
  float rv = -1;
  System.Web.HttpBrowserCapabilities browser = Request.Browser;
  if (browser.Browser == "IE")
    rv = (float)(browser.MajorVersion + browser.MinorVersion);
  return rv;
}

private void Page_Load(object sender, System.EventArgs e)
{
  string msg;
  double ver = getInternetExplorerVersion();
  if (ver > 0.0)
  {
    if (ver >= 6.0) 
      msg = "You're using a recent version of Internet Explorer.";
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  } 
  else
    msg = "You're not using Internet Explorer.";

  Label1.Text = msg;
}

Because the HttpBrowserCapabilities object parses the user-agent string for you, you can use the MajorVersion and MinorVersion properties to determine the current version of the browser. Unlike the JScript example, these properties are numeric; MajorVersion returns an integer value, and MinorVersion returns a double value. Depending on the language used to implement your ASP.NET application, however, you might still have to typecast these property values to the data type you're using. To illustrate, the C# example converts MajorValue to the floating point value returned by the function.

For more information about the HttpBrowserCapabilities object, see HOW TO: Determine the Browser Version in ASP.NET or Browser Sniffing in ASP.NET World Wide Web link.

Summary

If you must detect the browsers that view your Web sites, follow effective practices: plan for future browser releases, convert values appropriately, and design code to fail gracefully. Doing so will reduce the long-term maintenance of your site and help ensure that your site functions properly when viewed with newer versions of Internet Explorer and other browsers.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker