Na navigaci | Klávesové zkratky

The magic with conditional comments

This articles is actually an answer to e-mail by Honza Bien, who was asking me about the manipulations I was doing with conditional coments. Say, generally accepted idea is, that one kind of a comments (downlevel-hidden) is valid and the other (downlevel-revealed) is not. I tried to adapt those invalid comments the way that they would be valid. I'll explain the whole sequence.

First let's look at the official documentation.

Here we have so-called “downlevel-hidden” or if you wish “hidding” comment:

<!--[if IE 5]>
This is only seen by IE version 5
<![endif]-->

And also “downlevel-revealed” or “revealing” comment:

<![if !IE 5]>
This will be seen by all except of IE version 5
<![endif]>

Don't get confused by the content between the square brackets and let's generalise that for opening of commentary there are two official notations <!--[if ???]> and <![if ???]>, and further, for closing of commentary also two <![endif]--> and <![endif]>. That's an important point.

So that conditional comment with negation can surely be written like this:

<!--[if !IE]>
This can be seen by all except of IE
<![endif]-->

Perfect service now provides the syntax highlighter FSHL, thanx to which you can clearly see the flaw of the mentioned notation. Because, sentence “This can be seen by all except of IE” is seen by other browsers, but unfortunatelly as a part of a large comment. Which is of course worthless, isn't it?

Okay, so why don't we break that commentary?

<!--[if !IE]>
comment --> this is not <!-- comment again
<![endif]-->

After the optimization, conditional comment with negation will look like this:

<!--[if !IE]> -->
This can be seen by all except of IE
<!-- <![endif]-->

FSHL reveals that syntax is now correct and result is completely valid and functional conditional comment, which can be used for inserting of code not to be seen by IE.

You can try for yourself.

Kindly translated by creamd


phpFashion © 2004, 2024 David Grudl | o blogu

Ukázky zdrojových kódů smíte používat s uvedením autora a URL tohoto webu bez dalších omezení.