Internet Explorer 9对象检测

Internet Explorer 9 Object Detection

本文关键字:检测 对象 Explorer Internet      更新时间:2023-09-26

我正在搜索一个可以识别IE9的对象检测能力检查。你能帮我吗?

查看James Padolsey的片段:

// ----------------------------------------------------------
// 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字符串(这本身是不可靠的)——相反,它使用了条件注释,这些注释在IE中可靠地工作。

这可用于检测IE5-9。

不能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>

使用每个版本中引入的IE窗口对象的属性来区分IE版本:

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

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

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

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

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