Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using <comment> tag to reverse IE conditional comments

Posted on 2010-11-09
2
Medium Priority
?
314 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

963 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