Detect older IE versions
Asked Answered
C

4

14

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 the user-agent string (I'll need to figure out if it's IE and get the version number)?

Crowning answered 29/8, 2011 at 18:17 Comment(0)
P
16

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
}
Paton answered 29/8, 2011 at 18:21 Comment(2)
LOL I should have known that James Padolsey figured it out over a year ago :) thanks!Crowning
I remember looking at Rangy before... but I think it wasn't working with textareas, maybe I need to look at it again since a new version came out this month. Thanks again!Crowning
E
2

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.

Etalon answered 29/8, 2011 at 18:22 Comment(2)
Sometimes, there are things IE doesn't support that you can't use JavaScript to check for. Like CSS stuff.Homestretch
The plugin I am wortking on needs to position the caret inside of a textarea... IE9 seems to work well without any modification of the caret, but older versions just don't behave.Crowning
S
1

How about this:

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

Note that IE8 shows up as IE7

Schoolroom answered 19/11, 2011 at 4:51 Comment(3)
I'm trying to avoid browser sniffing as mentioned in my question.Crowning
$.browser is deprecated from 1.3 and removed from 1.9Countervail
I just want the future readers to know that the answer they are looking at might not work now/in the future. I fail to understand your point of frustrationCountervail
C
1

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]>
Catto answered 14/5, 2015 at 6:23 Comment(1)
Yes, but I was asking how to do it programmatically with javascript... plugin authors can't expect every page to have conditional comments in place, so we have to add them on our own.Crowning

© 2022 - 2024 — McMap. All rights reserved.