开发者

Detect older IE versions

开发者 https://www.devze.com 2023-04-01 02:06 出处:网络
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.

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.

We've been discouraged from parsing the user-agent string and using $.browser.msie. The $.support method doesn't cover the problem either.

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 开发者_如何学Gothe user-agent string (I'll need to figure out if it's IE and get the version number)?


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.

Source: 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:

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.


How about this:

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

Note that IE8 shows up as IE7


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.

<!--[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]>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号