Wednesday, May 30, 2007

JavaScript Conditional Compilation.


Below are few inbuilt JS variable using which we can know some details of Client machine.

Predefined conditional compilation variables
for JavaScript
Variable Description
@_win32 Returns true if running on a Win32 system, otherwise NaN.
@_win16 Returns true if running on a Win16 system, otherwise NaN.
@_mac Returns true if running on an Apple Macintosh system, otherwise NaN.
@_alpha Returns true if running on a DEC Alpha processor, otherwise NaN.
@_x86 Returns true if running on an Intel Processor, otherwise NaN.
@_mc680x0 Returns true if running on a Motorola 680x0 processor, otherwise NaN.
@_PowerPC Returns true if running on a Motorola PowerPC processor, otherwise NaN.
@_jscript Always returns true.
@_jscript_build The build number of the JScript scripting engine.
@_jscript_version A number representing the JScript version number in major.minor format.

IE4 supports JScript 3.x
IE5.x supports JScript 5.5 or less
IE6 supports JScript 5.6

The version number reported for JScript .NET is 7.x.

@_debug Returns true if compiled in debug mode, otherwise false.
@_fast Returns true if compiled in fast mode, otherwise false.

In most cases, you probably will be limited to just using @_win and @jscript_build:

/*@cc_on
@if (@_win32)
document.write("OS is 32-bit. Browser is IE.");
@else
document.write("OS is NOT 32-bit. Browser is IE.");
@end
@*/

User defined Variables

You can also define your own variables to use within the conditional compilation block, with the syntax being:

@set @varname = term

Numeric and Boolean variables are supported for conditional compilation, though strings are not. For example:

@set @myvar1 = 35
@set @myvar3 = @_jscript_version

The standard set of operators are supported in conditional compilation logic:

  • ! ~
  • * / %
  • + -
  • << >> >>>
  • < <= > >=
  • == != === !==
  • & ^ |
  • &amp;amp;& |

You can test if a user defined variable has been defined by testing for NaN:

@if (@newVar != @newVar)
//this variable isn't defined.

This works since NaN is the only value not equal to itself.

References:

No comments: