我正在寻找可以识别 IE9 的对象检测功能检查。你能帮助我吗?
问问题
1769 次
3 回答
6
// ----------------------------------------------------------
// 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。
于 2012-02-01T10:51:43.947 回答
0
不是 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>
于 2012-02-01T10:52:15.557 回答
0
使用各个版本中引入的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)
于 2013-05-23T02:20:36.050 回答