【发布时间】:2012-02-01 10:47:57
【问题描述】:
我正在寻找可以识别 IE9 的对象检测功能检查。你能帮帮我吗?
【问题讨论】:
标签: javascript internet-explorer-9 object-detection capability
我正在寻找可以识别 IE9 的对象检测功能检查。你能帮帮我吗?
【问题讨论】:
标签: javascript internet-explorer-9 object-detection capability
通过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。
【讨论】:
不是 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 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)
【讨论】: