When would <!--[if !IE]> be used?

1.4k views Asked by At

I was pondering this question last night while debugging some things in IE7, when I typed in <!--[if IE7]>... it occured to me that I've seen <!--[if !IE]>.

Now what get me is that as far as I understand it, only IE recognises these conditional comments, so to to say if NOT IE makes no sense, does IE see this and say "Iam IE, so this doesn't apply to me?" or are people getting confused with which browsers can recognise it?

So my question is.

Where would you use <!--[if !IE]> and what is its purpose?

4

There are 4 answers

2
mingos On BEST ANSWER

AFAIK, other browsers treat conditional comments as comment blocks (ie, they aren't parsed at all). So, such a comment will make something happen (ie, include a CSS file) in anything EXCEPT Internet Explorer (so it's the inverse of the [if IE] conditional comments). Add all your CSS in such comments and see how IE behaves :P.

[EDIT]

OK, I just checked. This is indeed silly. Since the <style> tag is in the CC, other browsers don't parse it at all, so it effectively doesn't get included at all. IE indeed interprets this as "aha, I am IE so I need to skip this". The only reasonable explanation I can think of is Microsoft assuming at some point other browsers might start parsing conditional comments.

0
Paddy On

I would say it would be if you wanted to run a script that works in every browser other than IE, as IE would not run it...

0
Gerry On

To confirm your suspicion it is kinda pointless to put it like that in your HTML, as most or all browsers will ignore the content of the comment block.

It can be useful to use the negation operator when wanting to exclude a specific version though, like <!--[if !(IE 6)]> would be parsed in all versions of IE (starting IE5), but not in IE6.

http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx

3
Horia Dragomir On

You can use it with a special synax:

<!--[if !(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->

This means that IE ignores what is in between, but other browsers treat the conditional comments as closed comments and interpret what's in between.