【问题标题】:Internet Explorer 9 Object DetectionInternet Explorer 9 对象检测
【发布时间】:2012-02-01 10:47:57
【问题描述】:

我正在寻找可以识别 IE9 的对象检测功能检查。你能帮帮我吗?

【问题讨论】:

    标签: javascript internet-explorer-9 object-detection capability


    【解决方案1】:

    通过James Padolsey查看this snippet

    // ----------------------------------------------------------
    // A short snippet for detecting versions of IE in JavaScript
    // without resorting to user-agent sniffing
    // ----------------------------------------------------------
    // If you're not in IE (or IE version is less than 5) then:
    //     ie === undefined
    // If you're in IE (>=5) then you can determine which version:
    //     ie === 7; // IE7
    // Thus, to detect IE:
    //     if (ie) {}
    // And to detect the version:
    //     ie === 6 // IE6
    //     ie > 7 // IE8, IE9 ...
    //     ie < 9 // Anything less than IE9
    // ----------------------------------------------------------
    
    // UPDATE: Now using Live NodeList idea from @jdalton
    
    var ie = (function(){
    
        var undef,
            v = 3,
            div = document.createElement('div'),
            all = div.getElementsByTagName('i');
    
        while (
            div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
            all[0]
        );
    
        return v > 4 ? v : undef;
    
    }());
    

    之后,你可以这样使用它:

    if (ie == 9) {
      // It’s IE9!
      // Insert your code here
    }
    

    这里的好处是它不会嗅探 UA 字符串(它本身是不可靠的) - 相反,它使用条件 cmets,它在 IE 中可靠地工作。

    这可以用来检测IE5—9。

    【讨论】:

    • 当我在 ie9 中测试它时,它给了我“7”。
    • 为此+1,这是我会发布的,但似乎并不那么知名。需要注意的一点是 IE 10 将不支持条件 cmets,因此此技巧在 IE 的未来版本中将不起作用(请参阅blogs.msdn.com/b/ie/archive/2011/07/06/…
    • @TimDown 是的,我在帖子中确实提到它可以用来检测 IE5-9——但这可能太微妙了。
    • @MathiasBynens:我看到了这一点,但认为需要说明这一点。所以是的,太微妙了:)
    【解决方案2】:

    不是 100% 确定这是您所要求的,但如果您想检测有关访问者浏览器的信息,您可以检查 navigator.appVersion

    例子:

    <div id="example"></div>
    
    <script type="text/javascript">
    
    txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
    txt+= "<p>Browser Name: " + navigator.appName + "</p>";
    txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
    txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
    txt+= "<p>Platform: " + navigator.platform + "</p>";
    txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
    
    document.getElementById("example").innerHTML=txt;
    
    </script>
    

    【讨论】:

      【解决方案3】:

      使用每个版本中引入的IE window object的属性来区分IE版本:

      • IE >= 7:("onpropertychange" in document) &amp;&amp; (!!window.XMLHttpRequest)

      • IE >= 8:("onpropertychange" in document) &amp;&amp; (!!window.XDomainRequest)

      • IE >= 9:("onpropertychange" in document) &amp;&amp; (!!window.innerWidth)

      • IE >= 10:("onpropertychange" in document) &amp;&amp; (!!window.matchMedia)

      • IE >= 11:(!!window.msMatchMedia) &amp;&amp; (!window.doScroll)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-20
        • 2013-06-06
        • 1970-01-01
        • 2013-07-11
        • 2012-12-10
        • 1970-01-01
        相关资源
        最近更新 更多