HTML :

  • If your goal is to ensure Internet Explorer 8 clients receive content built and tested for Internet Explorer 7, use a greater than or equal to (>=) comparison rather than an equal to (=) comparison. Additionally, ensure that the document mode (such as quirks or strict) is compatible with Internet Explorer 8.

    Example
    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 >= 7.0 )
         msg = "You're using Internet Explorer 7 or Internet Explorer 8. I should send a quirks or strict mode document."
       else
         msg = "You should upgrade your copy of Internet Explorer.";
     }
     alert( msg );
    }
  • If you wish to target content exclusively to Internet Explorer 8—such as by sending a document in the latest rendering mode that follows CSS 2.1 guidelines—use a greater-than-or-equal-to (>=) comparison. An exact string match is not recommended because it is not "future proof." In other words, if there is an Internet Explorer 9, you will need to update your website at some future date to handle detecting that release.

    Example
    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 >= 8.0 )
       msg = "You're using Internet Explorer 8 or later. I should send you CSS 2.1 content."
      else
       msg = "You should upgrade your copy of Internet Explorer.";
     }
     alert( msg );
    }

Asp.net :
 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;
}
posted on 2008-04-08 14:00  沉默的心  阅读(555)  评论(0编辑  收藏  举报