检测较旧的IE版本
我需要检测用户是否运行jQuery插件的旧版IE(IE9很好),所以我不能控制HTML。检测较旧的IE版本
我们一直不鼓励解析用户代理字符串,并使用$.browser.msie
。 $.support
方法也不包括这个问题。
所以,我发现这是有效的,但我不确定这是否是“良好实践”。
$('body').append('<!--[if lte IE 8]><script>$("body").addClass("oldie");</script><![endif]-->');
var old_ie = $('body').is('.oldie');
您可以使用此方法或解析用户代理字符串棒(我需要弄清楚,如果是IE浏览器,并获得版本号)?
您可以运行此
var ie = (function() {
var undef, v = 3, div = document.createElement('div');
while (
div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
div.getElementsByTagName('i')[0]
);
return v > 4 ? v : undef;
}());
检测IE版本。
来源:http://ajaxian.com/archives/attack-of-the-ie-conditional-comment
然后
if (ie < 9) {
// do your stuff, for instance:
window.location = 'http://getfirefox.com'; // :p
}
如何:
if (!($.browser.msie && $.browser.version < 9.0)) {
// Apply only to IE8 and lower
}
注意,IE8表现为IE7
我试图避免在我的问题中提到的浏览器嗅探。 – Mottie
$ .browser已从1.3弃用,并从1.9删除 –
我只是想让未来的读者知道,他们正在寻找的答案可能现在/将来无法正常工作。我无法理解你的沮丧点 –
https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx 见上面的链接,这可能是官方的回答:) 简而言之,代码在HTML以下页。
<!--[if gte IE 9]>
<p>You're using a recent version of Internet Explorer.</p>
<script type="text/javascript">//do something</script>
<![endif]-->
<!--[if lt IE 9]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<script type="text/javascript">//do something</script>
<![endif]-->
<![if !IE]>
<p>You're not using Internet Explorer.</p>
<script type="text/javascript">//do something</script>
<![endif]>
是的,但我问的是如何用javascript以编程方式来做它......插件作者不能指望每个页面都有条件注释,所以我们必须自己添加它们。 – Mottie
LOL我应该知道James Padolsey在一年前就知道了:)谢谢! – Mottie
我记得之前看过Rangy,但是我认为它不能和textareas一起工作,也许我需要在本月推出新版本之后再看一遍。再次感谢! – Mottie