【问题标题】:Reliably Detect IE7 in IE6 document mode在 IE6 文档模式下可靠地检测 IE7
【发布时间】:2020-10-06 21:54:24
【问题描述】:

我知道这是一个非常具体、晦涩难懂的问题,因此请不要用用例或最佳实践@我。我知道要测试特定功能的支持。我知道 IE 已经过时了。

在 Windows Service Pack Update (SP3) 之后,IE6 有时会报告为 IE7。我可以通过使用条件编译和功能检测XMLHttpRequest 可靠地测试伪装成 IE7 的 IE6,但我很确定 IE6 文档模式(或更早的模式,老实说)中的 IE7 也会对XMLHttpRequest 测试为阴性。

有没有办法可靠地确定 IE 的浏览器引擎是 IE6 还是 7,而不管文档模式如何?

【问题讨论】:

    标签: internet-explorer-7 internet-explorer-6


    【解决方案1】:

    文档模式是 IE8 之前不支持的功能。

    因此,您需要做的就是检查 IE7 支持但 IE6 不支持的功能。

    【讨论】:

      【解决方案2】:

      根据XMLHttpRequest browser compatibility,我们可以看到XMLHttpRequest 支持IE 7+。如果我们想在 IE 5 中使用它,我们应该通过 ActiveXObject('Microsoft.XMLHTTP') 来使用它。因此,我们可以使用此功能来检测 IE 7 浏览器。

      对于伪装成 IE7 的 IE6,我想您可能正在使用 userAgent 字符串来检测 IE 浏览器版本。经过我这边的UserAgent测试,目前看来,如果我们将F12 developer Emulation改为IE 5和IE 7模式,UserAgent字符串如下:

      • IE 7 模式:“mozilla/4.0(兼容;msie 7.0;windows nt 10.0;wow64;trident/7.0;.net4.0c;.net4.0e;.net clr 2.0.50727 ;.net clr 3.0.30729;.net clr 3.5.30729) "

      • IE 5 模式:“mozilla/4.0(兼容;msie 7.0;windows nt 10.0;wow64;trident/7.0;.net4.0c;.net4.0e;.net clr 2.0.50727 ;.net clr 3.0.30729;.net clr 3.5.30729) "

      正如我们所见,他们使用相同的 UserAgent,似乎在 IE 5 模式下,Internet Explorer 将其 User Agent 更改为“MSIE 7.0”。

      我也尝试使用Conditional Comment来检测IE浏览器版本,似乎在IE 5+上运行良好。

      请检查以下示例:

      <center>
          <h1 style="color:blue">How to detect IE</h1>
          <script>
              //detects if user is using Internet Explorer based on the userAgent
              //returns version of IE or false, if browser is not IE
              //Function to detect IE or not
              function IEdetection() {
                  var ua = window.navigator.userAgent;
                  var msie = ua.indexOf('MSIE ');
                  if (msie > 0) {
                      // IE 10 or older, return version number
                      return ('IE ' + parseInt(ua.substring(
                          msie + 5, ua.indexOf('.', msie)), 10));
                  }
                  var trident = ua.indexOf('Trident/');
                  if (trident > 0) {
                      // IE 11, return version number
                      var rv = ua.indexOf('rv:');
                      return ('IE ' + parseInt(ua.substring(
                          rv + 3, ua.indexOf('.', rv)), 10));
                  }
                  var edge = ua.indexOf('Edge/');
                  if (edge > 0) {
                      //Edge (IE 12+), return version number
                      return ('IE ' + parseInt(ua.substring(
                          edge + 5, ua.indexOf('.', edge)), 10));
                  } 
                  // User uses other browser
                  return ('Not IE');
              }
              var result = IEdetection();
              document.write("UserAgent: <br/>");
              document.write(window.navigator.userAgent);
      
              document.write("<br/>Using UserAgent detection, result: <br/>");
              document.write(result);
      
      
              var ie = (function () {
                  if (window.ActiveXObject === undefined) return null; //Not IE
                  if (!window.XMLHttpRequest) return 6;
                  if (!document.querySelector) return 7;
                  if (!document.addEventListener) return 8;
                  if (!window.atob) return 9;
                  if (!document.__proto__) return 10;
                  return 11;
              })();
              document.write("<br/>Using feature detection, Result:<br/>");
              document.write(ie);
          </script>
      
      
      <!--[if IE 5]>
      <p class="ieversion" data_version="5">You are using Internet Explorer 5.</p>
      <![endif]-->
      <!--[if IE 7]>
      <p class="ieversion" data_version="7">You are using Internet Explorer 7.</p>
      <![endif]-->
      <!--[if IE 9]>
      <p class="ieversion" data_version="9">You are using Internet Explorer 9.</p>
      <![endif]-->
      
          <script>
              if (!document.getElementsByClassName) {
                  document.getElementsByClassName = function (search) {
                      var d = document, elements, pattern, i, results = [];
                      if (d.querySelectorAll) { // IE8
                          return d.querySelectorAll("." + search);
                      }
                      if (d.evaluate) { // IE6, IE7
                          pattern = ".//*[contains(concat(' ', @class, ' '), ' " + search + " ')]";
                          elements = d.evaluate(pattern, d, null, 0, null);
                          while ((i = elements.iterateNext())) {
                              results.push(i);
                          }
                      } else {
                          elements = d.getElementsByTagName("*");
                          pattern = new RegExp("(^|\\s)" + search + "(\\s|$)");
                          for (i = 0; i < elements.length; i++) {
                              if (pattern.test(elements[i].className)) {
                                  results.push(elements[i]);
                              }
                          }
                      }
                      return results;
                  }
              }
              var item = document.getElementsByClassName("ieversion");
              if (item.length > 0) {
                  document.write("<br />Using Conditional comment + JavaScript, result:<br/>");
                  document.write("IE version: " + item[0].getAttribute("data_version"));
              }
          </script>
      </center>
      

      结果如下:

      由于Conditional comment支持从IE 5到IE 9,但在Internet Explorer 10和11中不支持。因此,您可以使用条件注释来检测IE 5 ~ IE 9,并使用UserAgent来检测IE 10 和 IE 11。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-29
        • 2015-01-15
        • 2013-06-08
        • 2012-04-02
        相关资源
        最近更新 更多