简体   繁体   English

检测较旧的IE版本

[英]Detect older IE versions

I need to detect if the user is running an older version of IE (IE9 is fine) from a jQuery plugin, so I won't have control over the HTML. 我需要检测用户是否从jQuery插件运行旧版本的IE(IE9很好),所以我无法控制HTML。

We've been discouraged from parsing the user-agent string and using $.browser.msie . 我们不鼓励解析用户代理字符串并使用$.browser.msie The $.support method doesn't cover the problem either. $.support方法也没有涵盖这个问题。

So, I figured out that this works, but I'm not sure if it is "good practice". 所以,我发现这有效,但我不确定它是否是“良好做法”。

$('body').append('<!--[if lte IE 8]><script>$("body").addClass("oldie");</script><![endif]-->');
var old_ie = $('body').is('.oldie');

Would you use this method or stick with parsing the user-agent string (I'll need to figure out if it's IE and get the version number)? 你会使用这种方法还是坚持解析用户代理字符串(我需要弄清楚它是否是IE并得到版本号)?

You can run this 你可以运行它

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;
}());

to detect the version of IE. 检测IE的版本。

Source: http://ajaxian.com/archives/attack-of-the-ie-conditional-comment 资料来源: http //ajaxian.com/archives/attack-of-the-ie-conditional-comment

And then 接着

if ( ie < 9 ) {
    // do your stuff, for instance:
    window.location = 'http://getfirefox.com'; // :p
}

You didn't explicitly mention in your question why you had a specific need to detect for IE9, so in general the following advice holds: 您没有在您的问题中明确提到为什么您需要检测IE9,因此通常以下建议成立:

Rather than detecting for a specific browser / version, you should instead be detecting for specific features. 您应该检测特定功能,而不是检测特定的浏览器/版本。 Modernizr is a good place to start for help with this. Modernizr是一个开始寻求帮助的好地方。

https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx See the link above, this might be the official answer :) Briefly, code the following in your html page. 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]>

How about this: 这个怎么样:

if (!($.browser.msie && $.browser.version < 9.0)) {
    // Apply only to IE8 and lower
}

Note that IE8 shows up as IE7 请注意,IE8显示为IE7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM