Pre-processing - @if( ... ) ... (Pre-processor)

Conditionally include a block of code.

Availability:

JScript - 3.0
Internet Explorer - 4.0
JavaScript syntax:IE@if(aCondition) someCode @elif(aCondition) someCode @else someCode @end
IE@if(aCondition) someCode @elif(aCondition) someCode @end
IE@if(aCondition) someCode @else someCode @end
IE@if(aCondition) someCode @end
Argument list:aConditionA condition that yeilds a Boolean value
someCodeA block of code that is conditionally included

The @if() directive is very flexible, having two optional associated directives (@elif() and @else), which provide a variety of different configurations.

The simplest form is where a section of code is included or not. That would be organized like this:

   @if(anExpression)

   ...

   someCode

   ...

   @end

If the expression yields a true value then the code will be included, otherwise it will be skipped, as if it had been completely commented out.

The next simplest form is to place an alternative section of code in the conditional section and have that used when the expression yields a false value. That would be laid out like this:

   @if(anExpression)

   ...

   someCode

   ...

   @else

   ...

   someOtherCode

   ...

   @end

The result of the expression selects one block of code or the other.

A third and somewhat more complex configuration allows a series of conditions to be tested. This is somewhat like a switch tree although it is a little less elegant in its presentation. You can test for several conditions and include an appropriate block of code for the one that holds true. However, only one will be selected. This is accomplished with the @elif() directive. This is only tested when a prior @if() or @elif() test proves false.

Here is an configuration that tests for three possible conditions:

   @if(anExpression)

   ...

   someCode

   ...

   @elif(anExpression)

   ...

   someCode

   ...

   @elif(anExpression)

   ...

   someCode

   ...

   @end

Note that in this form, there is no alternative block of code associated with an @else directive. One of these expressions must prove true for any code to be included. If none of them prove true, then the entire conditional block is ignored.

The final configuration provides a fall-back alternative code block and is constructed like this:

   @if(anExpression)

   ...

   someCode

   ...

   @elif(anExpression)

   ...

   someCode

   ...

   @elif(anExpression)

   ...

   someCode

   ...

   @else

   ...

   someOtherCode

   ...

   @end

You may be able to nest these directives but it is recommended that you avoid complexity when building conditional code structures as it make thes code more difficult to maintain.

If you intend to hide this directive inside some comments, it must be done like this:

   /*@if(anExpression)

   ...

   someCode

   ...

   @elif(anExpression)

   ...

   someCode

   ...

   @elif(anExpression)

   ...

   someCode

   ...

   @else

   ...

   someOtherCode

   ...

   @end @*/

See also:Conditional code block, Pre-processing, Pre-processing - /*@ ... @*/