How to comment out IE conditional comments? - html

I'm just wondering if it's possible to comment out IE conditional comments themselves (for testing purposes)? The following does not work:
<!-- <!--[if IE 7]> some code <![endif]--> -->
Thanks in advance! flexx

No, it isn't possible.
SGML/HTML/XML/XHTML comments cannot be nested.

I think you can just insert something to make them invalid:
<!--\[if IE 7]> some code <!\[endif]-->

<!--[if IE 70]> some code <![endif]-->
:)

If I were you I'd use a template systems like Template Toolkit and then Just include or exclude the comment conditionally, based on some variable you could set at runtime.
Template Toolkit http://template-toolkit.org/

If you are sending that through a server such as .asp or aspx then of course you could comment that out server side.

You can't nest comments, but you can comment them out by adding an extra <!-- to the beginning. Your example from above changes from <!-- <!--[if IE 7]> some code <![endif]--> --> to <!-- <!--[if IE 7]> some code <![endif]-->. Just take out the extra --> and it should comment out that pesky conditional.
Tested successful in Firefox 3.5.2 and IE 7.0.5730.13CO

Related

Conditional comments syntax

Can anyone explain me the difference between first and second conditional comment syntax?
<!--[if lt IE 8]><html class="no-js lt-ie8"><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]-->
Why the second one use use the <!--> syntax before <html> tag? Can I use the following syntax with the same result?
<!--[if lt IE 8]><html class="no-js lt-ie8"><![endif]-->
<!--[if gt IE 8]><html class="no-js"><![endif]-->
Thanks in advance!
No, your second setup will not be the same as your first.
In the first, where <!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]--> is used, it will let all other browsers (the one's that does not understand conditional comments) to have <html class="no-js">, which the second setup won't
Src: https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx
General accepted syntax is (was)
open tag : <!--[if condition version]>
end tag : <![endif]-->
Best guess is that its an attempt at a bogus comment. Two dashes after <! make it a comment node in the DOM. Refer this answer
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]-->
^^^^^ ^^^^
||||| ||||
________Bogus Comment________
It doesn't really make any difference. The last one will probably render as a <!-- comment --> or a comment + condition ... not sure.

How do <scripts> load inside of comment tags?

The following code is in head section of a page:
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
Shouldn't the comment tags stop this script from being run?
It's called Conditional Comments
https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx
http://www.quirksmode.org/css/condcom.html
Only works with Internet Explorer.
IE is smart with these style comments (IE conditional comments) and knows to treat them as normal HTML.

Excluding IE 6/7 traffic

I would like to ask if there is any easy way of displaying different page for IE6/7 users who enter a website.
Like a redirect from example.com to example.com/ie7
Unfortunately IE7 doesn't like the website I made so I want to display miniversion of the original website, I have put too much effort into the original to downgrade it now.
Will this line always work? On every version/build of IE7? Or is it more complicated?
I want to be sure that 100% of IE7 traffic gets redirected.
<!--[if IE]><meta http-equiv="refresh" content="0;URL=http://www.example.com/ie7"><![endif]-->
You can use a different CSS on the same page to get a simpler result
<!--[if lt IE 8]>
<link href="/IE7style.css" rel="stylesheet" type="text/css" media="all" />
<![endif]-->
Just overwrite all the styling needed to make IE7 happy
to serve content to IE7 , you need to set the version in conditionnal comments.
<!--[if IE 7 ]><p>I'm IE 7</p><![endif]-->
IE7 and lower :
<!--[if lte IE 7 ]><p>I'm IE 7 at the most.</p><![endif]-->
Where lte means Lighter Than or Equal
The better way is
<!--[if IE 7]>
<script type="text/javascript">
window.location = "http://www.example.com/ie7";
</script>
<![endif]-->
This should be done server side. You can use something like ua-parser to detect ie <=7, and redirect to a new site accordingly.

Extra text issue in IE7 only

I am getting extra text <![endif]--> in IE7 but on all other browsers does not show.
I searched this code in all files but didn't find it. I am using Drupal-7.
Please check this link here
You aren't writing those end tags correctly in some areas. Some places you put this:
<!--<![endif]-->
Change that to this:
<![endif]-->
Looks like there is an extra <![endif]--> here
<!--[if lt IE 8]>
<link type="text/css" rel="stylesheet" href="http://indivar.bubblespan.com/sites/default/files/css/css_SCgABqUC566L7vt9DqHzb_pIQdEOcwHOijyO95sLhUI.orig.css" media="all" />
<![endif]-->
<![endif]-->
Although it is difficult to tell with all the conditional comments you are using.
Better to use fewer comments and to combine content within the conditional comments as needed.
Please try to re-install conditional css module which you are using for drupal.

CSS: How to load a css file for all browsers except IE?

how can I load a css file only if the browser is not IE ?
In other terms, I want to load a css file only if it is Safari, Firefox o Chrome.
The opposite of this:
<!--[if IE 8]>
<![endif]-->
thanks
<!--[if !IE]>-->
...
<!--<![endif]-->
Don't use the following. It is invalid HTML:
<![if !IE]>
...
<![endif]>
I believe based off this article http://msdn.microsoft.com/en-us/library/ms537512%28VS.85%29.aspx you can do this:
<![if !IE]>
Other browser code here
<![endif]>
Edited the above to be valid. The link at microsoft shows the above, but RoToRa is correct that it is invalid and should be (note, contrary to RoToRa, you don't need the extra "!" before the "endif" inside the brackets):
<!--[if !IE]>-->
Other browser code here
<!--[endif]-->
In addition to the plethora of correct answers above, it's worth asking yourself why you're doing this - it's usually more correct to include a file only for IE since it is only IE which meaningfully deviates from the standards.
The inverse form also has the benefit of being considerably more readable, imho. :)
<!--[if !IE]>
<![endif]-->
more info here