Internet Explorer 9对象检测

问题描述:

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

通过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字符串(,本身并不可靠) - 相反,它使用条件注释,它在IE中可靠地工作。

这可以用来检测IE5-9。

+0

当我在ie9中测试它时,它会给我“7”。 – 2012-02-01 10:57:08

+1

为此+1,这是我会发布,似乎不那么知名。有一点需要注意的是,IE 10不会支持条件注释,所以这个技巧在IE的未来版本中不起作用(请参阅http://blogs.msdn.com/b/ie/archive/2011/07/06/html5 -parsing-in-ie10.aspx) – 2012-02-01 10:58:45

+0

它无法正常工作:( – 2012-02-01 11:00:30

不是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> 
+0

这是不建议的方式看看这里http://www.quirksmode.org/js/support.html – 2012-02-01 10:57:43

使用属性在每个版本中引入的IE window object的区分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)