views:

250

answers:

4

I want to use a JavaScript only for all IE version 6,7 and 8.

+1  A: 

You could do this to prevent it from applying to future versions of IE

<!--[if lt IE 9]>
Content for IE less than version 9 goes here
<![endif]-->
yong
Its easier to read your code if you indent it at least 4 spaces so it gets put in a `pre` block.
Doug Neiner
Can we also use this <!--[if IE]> <h2>You are using Internet Explorer.</h2> <![endif]--> ?
metal-gear-solid
@Jitendra, yes you can. (See my answer below. Or above, depending on how you're sorting.)
Martha
@dcneiner: I formatted the answer with 4 space indents.
Asaph
+6  A: 

Nested conditions (WRONG):

<!--[if lte IE 8]> 

<!--[if gte IE 6]> 

<!-- your stuff here -->

<![endif]-->

<![endif]-->

EDIT: As Martha highlighted, nested condition don't work, use " & ":

<!--[if (lte IE 8) & (gte IE 6)]> 


<!-- your stuff here -->


<![endif]-->

Other examples from MSDN:

<!--[if IE]><p>You are using Internet Explorer.</p><![endif]-->
<![if !IE]><p>You are not using Internet Explorer.</p><![endif]>

<!--[if IE 7]><p>Welcome to Internet Explorer 7!</p><![endif]-->
<!--[if !(IE 7)]><p>You are not using version 7.</p><![endif]-->

<!--[if gte IE 7]><p>You are using IE 7 or greater.</p><![endif]-->
<!--[if (IE 5)]><p>You are using IE 5 (any version).</p><![endif]-->
<!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using IE 5.5 or IE 6.</p><![endif]-->
<!--[if lt IE 5.5]><p>Please upgrade your version of Internet Explorer.</p><![endif]-->

<!--[if true]>You are using an <em>uplevel</em> browser.<![endif]-->
<![if false]>You are using a <em>downlevel</em> browser.<![endif]>

<!--[if true]><![if IE 7]><p>This nested comment is displayed in IE 7.</p><![endif]><![endif]-->
o.k.w
You can't nest regular html comments - the first 'end comment' tag will be considered the end of the comment, and everything after it will be displayed. The extra 'end comment' tags will be ignored as invalid/unknown. I don't think this is any different with IE conditional comments. (If nothing else, nesting them will make your page break in non-IE browsers.)
Martha
@Martha, you are right! I'll amend my answer.
o.k.w
I wasn't aware you could nest these. Great to know. +1
MitMaro
o.k.w
MitMaro, see my note above - you actually *can't* nest html comments. (IE conditional comments work because to non-IE browsers, they look like regular ol' comments.)
Martha
I think it's enough for my needs <!--[if lte IE 8 ]> <p>IE 8 and lower version</p> <![endif]-->
metal-gear-solid
MitMaro
+1  A: 

You can target conditional comments at Internet Explorer regardless of version:

<!--[if IE]> IE stuff goes here <![end if]-->

See http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx

Martha
A: 

This one blew me away, but check it out:

var IE = /*@cc_on!@*/false;

Source

fauxtrot
Sorry, just realized you wanted the conditional comment usage from your title. *hides in shame* I'll try to read the title better next time. Good luck. *note to self, no SO after 10:30
fauxtrot
Is that like "no sewing after midnight"? :)
Martha