Solved

Using <comment> tag to reverse IE conditional comments

Posted on 2010-11-09
2
304 Views
Last Modified: 2012-06-21
I want an image to be used in IE, but a different one to be used in other browsers. I can get the IE image to display only in IE by using conditional comments. I can also get the non IE image not to display in IE by using the non-standard <comment> tag.
The problem with this is that it makes my HTML invalid. My solution was to enclose the <comment> tag and its corresponding </comment> in conditional comments, so that Firefox et al ignored it, making the page valid once again.
The idea seems sound to me, but sadly the closing </comment> does not work in a conditional comment. Probably because IE is waiting for the closing comment before it evaluates the condition. My code is below. Any solutions to this?

Thanks,

Olly.
<!--[if IE]>

  <img src="img/IEImage.jpg" alt="This image should be displayed only in IE">

  <comment>

<![endif]-->

  <img src="img/OtherImage.png" alt="This image should never be displayed in IE">

<!--[if IE]>

  </comment>

<![endif]-->

Open in new window

0
Comment
Question by:ollyatstithians
  • 2
2 Comments
 
LVL 10

Author Comment

by:ollyatstithians
ID: 34094722
BTW I am looking for a way to choose one image over another in an <img> tag. I am aware I can use CSS as a workaround.
0
 
LVL 10

Accepted Solution

by:
ollyatstithians earned 0 total points
ID: 34095190
So with a lot of fiddling I have found a way to do conditional comments to be ignored by IE but not other browsers that actually validates.
Essentially you need to close the comment and re-open it either side of the IE ignored code.
<!--[if !IE]>-->

  <p>This will be rendered by non-IE5+ browsers. The conditional comments will appear as 

    HTML comments to the validator and other browsers</p>

<!--<![endif]-->

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now