Solved

Using <comment> tag to reverse IE conditional comments

Posted on 2010-11-09
2
308 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

776 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