views:

1841

answers:

6

Is there any option for using IF-ELSE conditioning in HTML tags

 <if true>  do something   </if>  
 <else>     do something   </else>
+6  A: 

Not to be pedantic, but HTML is a markup language, and isn't useful for conditional logic.

That being said, it sounds like what you're looking for is a bit of javascript. If you could add a bit more detail to your question, I could elaborate on how you could use javascript to do tasks with conditional logic.

wilsoniya
I don't think you're being pedantic ... spot on - HTML for markup only.
Jayx
+1  A: 

There is, but it's really only used in IE to distinguish between different versions:

<!--[if IE6]>
    Things here!
<![endif]-->
hydrapheetz
+3  A: 

HTML was designed for document layout so the noscript and noframes are about as close as HTML gets to handling conditionals. You could conceivably approach this problem with javascript.

<div id='if-part' style='visibility: hidden;'>do something</div>
<div id='else-part' style='visibility: hidden'>do something</div>

<script>
    var node;
    if(true) {
        node = document.getElementById('if-part');
    }
    else {
        node = document.getElementById('else-part');
    }
    node.style.visibility = 'visible';
</script>

of course this only works if the client has javascript turned on.

Kevin Loney
What about the <noscript> tag? Seems like it might work for the else condition.
hydrapheetz
true, but only if javascript is disabled client side.
Kevin Loney
+3  A: 

Conditional rendering of HTML is not a new concept, but it cannot be done using HTML exclusively. You would need to use either client side scripting or server side code to provide the conditional logic that would render your HTML accordingly.

Cerebrus
A: 

JavaScript ... conditional comments have no place in HTML. Even the old IE6 & IE7 comments were not quite valid either.

Jayx
+2  A: 

As has been said in other posts, HTML does not support conditional logic. You have two choices here:

1) Generate the HTML dynamically using technologies such as PHP or XSLT

2) Modify the HTML DOM after the fact using Javascript

17 of 26