Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using <comment> tag to reverse IE conditional comments

Posted on 2010-11-09
2
Medium Priority
?
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
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…

722 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