Dynamic HTML: The Definitive Reference, 2rd Ed.Dynamic HTML: The Definitive ReferenceSearch this book

12.8. Miscellaneous Statements

 
//, /*...*/NN 2 IE 3 ECMA 1

These are comment statements that let you enter nonexecuting text in a script. Any text following the // symbol anywhere in a statement line is ignored by the language interpreter. The next line of script, unless it begins with another // symbol, is interpreted by the browser.

For multiline comment blocks, you can begin a block with the /* symbol. Comment blocks may run any number of lines. The block is closed with the */ symbol, after which the interpreter engages subsequent statements.

Example

// convert temp from C to F

/*
many lines
of 
comments
*/
 
@cc_on, @if, @end, @setNN n/a IE 4(Win) ECMA n/a

IE for Windows includes a scripting feature called conditional compilation. It is a mode that, once turned on via the @cc_on statement, allows JScript statements to run under conditions that are testable within this conditional environment. If you surround conditional compilation statements by JavaScript comments, the conditional statements run only in IE 4 or later for Windows, while not conflicting with other browsers.

The "conditional" part comes from numerous global properties (all preceded with the @ symbol) that reveal environmental properties, such as script engine version, operating system, and CPU type. All of this information is available from the navigator object's properties on a wide range of browsers, so this is not unique information available only to this conditional environment.

To engage conditional compilation, include the following statement in your script:

/*@cc_on @*/

This is a one-way toggle: once the mode is turned on, it can't be turned off in the current page.

The following fragment shows how the @if and related statements display some environmental information in the window's status bar if the browser is running JScript Version 5.6 or later (IE 6 or later):

/*@cc_on @*/
/*@if (@_jscript_version >= 5.6 && @_x86)
    status = "Now running JScript version " + @_jscript_version + 
    " with Intel inside.";
   @else @*/
    status = "Have a nice day.";
/*@end @*/

The @set statement lets you assign a numeric or Boolean value (no strings) to a variable (a variable with an @ prefix) within a conditional compilation section:

@set @isOK = @_win32

Once initialized, that variable (including its otherwise unacceptable identifier) can be used in script statements throughout the page. Note that the Visual Basic-inspired syntax of @ statements in conditional compilation statements does not permit semicolons at the end of statements.

On the one hand, conditional compilation could be useful for IE-only deployment to screening older IE versions from new language features that would generate compilation errors (such as try-catch constructions) because such statements compile only under very controllable version situations. In a multibrand browser development shop, however, at most you might find application for IE-only debugging purposes, but probably not for actual application deployment.

Example

See the discussion above.

 
functionNN 2 IE 3 ECMA 1

The function keyword begins a named function definition. For anonymous functions, see the Function object.

Example

function myFunc(arg1, arg2) {
    // function statements here
} 
 
varNN 2 IE 3 ECMA 1

A keyword that defines the creation of a new variable. Although the keyword is optional for global variables (those not declared or initialized inside a function), it is good form to use this keyword for each new variable. Using the var keyword inside a function makes the variable local to statements inside the function.

You may simply declare one or more variable names, in which case their initial values are null. Or you can also initialize a new variable with a value.

Example

var a, b, c;
var myName = "Susan";


Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.