Forum Moderators: open

Message Too Old, No Replies

Neat trick with IE Conditional Statements

Let other browsers read them, too

         

MatthewHSE

9:55 pm on Nov 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This afternoon, I "discovered" a way to allow non-IE browsers access to the contents of IE Conditional Statements. Probably others have discovered this "trick" too, and undoubtedly it's been published on the web somewhere, but I'd never heard of it before. So, for others who also haven't heard of this, I thought it might be a helpful tip to know.

The trick is fairly simple. Everyone who's used IE Conditional Statements is familiar with the concept:

<!--[if gte IE 5.5000]>
<p>Only IE version 5.5 and up will render this paragraph.</p>
<![endif]-->

However, what if we only wanted to hide that paragraph from IE version 5.0 and below, while still allowing non-IE browsers to render the paragraph? The trick is to simply fool the other browsers into thinking the conditional statement has been ended, before you get to the content of the conditional:

<!--[if gte IE 5.5000]><!-->
<p>All browsers except IE versions lower than 5.5 will render this paragraph.</p>
<![endif]-->

By adding the

<!-->
right after opening the conditional statement, non-IE browsers think the comment has been finished. They then render the comment's content as usual. The close of the comment is not rendered by other browsers because it is between the < > symbols. IE will treat the whole thing like a normal conditional statement.

As far as I can tell, this is a great way to hide problematic code from the lower versions of IE, while still allowing other browsers to render it.

MatthewHSE

7:56 pm on Nov 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another very useful feature to this concept is that it allows you to give content only to non-IE users. We all know about choosing versions of IE except certain versions, e.g.,

<!--[if! IE 6]>

The contents of that conditional comment will be read by all versions of IE, as long as they're not version 6. By simply leaving off a version number, this principle can be used to cause all versions of IE to ignore the contents of the comment:

<!--[if! IE]><!-->This will show to everyone except Internet Explorer users.<![endif]-->

Obviously this technique should be used sparingly, but I can see certain applications where it would be quite handy.

tedster

8:08 pm on Nov 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Those are some very nifty tips -- thanks much!

DanA

9:24 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



Nice but html does not validate when using

<!--[if! IE]><!-->This will show to everyone except Internet Explorer users.<![endif]-->

MatthewHSE

1:01 pm on Nov 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's one drawback I hadn't even thought to check. Although, if I have to use IE conditional statements in the first place, validation normally isn't at the top of my list of priorities! ;)