The conditional comment in JScript is explained in detail

  • 2020-06-01 08:14:03
  • OfStack

JScript can use the following statement to control the compilation of the script based on the value of the conditional compilation variable. You can define your own variables using either the variables provided by JScript or the @set directive or /define command-line options.

@cc_on
Enable conditional compilation support.

@if
Conditional execution of 1 group of statements based on the value of the expression.

@set
Create variables that use conditional compilation statements.

The @cc_on, @if, or @set statements enable conditional compilation. Some typical USES of conditional compilation include the use of new features in JScript, embedding debugging support in a script, and tracking code execution.

When writing scripts that are run by the Web browser, conditional compilation code is always placed in comments. Therefore, hosts that do not support conditional compilation can ignore this code. This is an example.


/*@cc_on @*/
/*@if (@_jscript_version >= 5)
document.write("JScript Version 5.0 or better.<BR>");
@else @*/
document.write("You need a more recent script engine.<BR>");
/*@end @*/


This example USES special annotation delimiters that are used only after the @cc_on statement activates conditional compilation. A script engine that does not support conditional compilation displays a message suggesting that a new script engine is needed without errors. The engine that supports conditional compilation compiles the first or second document.write, depending on the version of the engine. Note that the 7.x version stands for JScript.NET. For more information, see checking browser capabilities.

Conditional compilation is also useful for server-side scripts and command-line programs. In these applications, you can use conditional compilation to compile other functions into a single program for easy analysis in debug mode.


The following predefined variables can be used for conditional compilation.

@_win32
true if you are running on an Win32 system and you do not specify the /platform option or the /platform:anycpu option. Otherwise, NaN.

@_win16
If running on the Win16 system, true; Otherwise, it is NaN.

@_mac
true if running on Apple Macintosh system; Otherwise, NaN.

@_alpha
true if running on DEC Alpha processor; Otherwise, it is NaN.

@_x86
true if it is running on an Intel processor and there is no /platform option specified or the /platform:anycpu option specified. Otherwise, NaN.

@_mc680x0
true if running on Motorola 680x0 processor; Otherwise, it is NaN.

@_PowerPC
true if running on an Motorola PowerPC processor; Otherwise, NaN.

@_jscript
Always true.

@_jscript_build
The internal version number of the JScript scripting engine.

@_jscript_version
The number that represents the JScript version number in major.minor format.

@_debug
If compiled in debug mode, true; Otherwise, false.

@_fast
true if compiled in fast mode; Otherwise, false.

Pay attention to
The JScript.NET report has a version number of 7.x. JScript 8.0 reports version 8.x.

Conditional compilation must be turned on before using conditional compilation variables. The @cc_on statement turns on conditional compilation. Conditional compilation variables are typically used in scripts written for the Web browser. Conditional compilation variables are rarely used in scripts written for ASP or ASP.NET pages or command-line programs because compiler compatibility can be determined using other methods.

When writing scripts for web pages, always keep conditional compilation code in comments. This way, hosts that do not support conditional compilation can ignore the code. This is an example.



/*@cc_on
  document.write("JScript version: " + @_jscript_version + ".<BR>");
  @if (@_win32)
     document.write("Running on 32-bit Windows.<BR>");
  @elif (@_win16)
     document.write("Running on 16-bit Windows.<BR>");
  @else
     document.write("Running on a different platform.<BR>");
  @end
@*/


Related articles: