Adding Client-Side Logic To WAP Using WMLScript
WMLScript Language Syntax
WMLScript syntax is based on the ECMAScript programming language. Unlike ECMAScript, however, the WMLScript specification also defines a bytecode and interpreter reference architecture for optimal utilization of current narrowband communications channels and handheld device memory requirements. The following bullets help summarize some basic syntactical features of the language:
- The smallest unit of execution in WMLScript is a statement and each statement must end with a semicolon (;).
- WMLScript is case-sensitive.
- Comments can either be single-line (beginning with //) or multi-line (bracketed by /* and */). This syntax is identical to both C++ and Java.
- A literal character string is defined as any sequence of zero or more characters enclosed within double ("") or single (‘) quotes.
- Boolean literal values correspond to true and false.
- New variables are declared using the var keyword (i.e.
WMLScript is a weakly typed language. This means that no type-checking is done at compile- or run-time and no variable types are explicitly declared. Internally, the following data types are supported:
The programmer does not need to specify the type of any variable; WMLScript will automatically attempt to convert between the different types as needed. One other point to note is that WMLScript is not object-oriented (such as Java or C++). Therefore, it is impossible to create your own user-defined data types programmatically.
WMLScript supports a variety of operators that support value assignment operations, arithmetic operations, logical operations, string operations, comparison operations, and array operations. For more information on the wide variety of WMLScript operators, see the WMLScript specification.
Flow Control Statements
Related WMLScript statements can be executed together as a unit known as a function. A function declaration has the following syntax:
extern function identifier(FormatParameterList) Block ;
extern keyword is optional and is used to specify a function that can be called from outside the current compilation unit in which the function is defined. A sample WMLScript function declaration looks like this:
function RunTime(distance, speed)
var time = distance / speed;
The above example simply takes two input variables,
speed, and uses them to calculate a time variable. The return keyword is then used to return a value.
When calling a function included with one of the WMLScript standard libraries (see below), the library name must be included with the function call. For example, to call the String library’s
length() function, use the following syntax:
var a = String.length("1234567890");
Next: The WMLScript Standard Libraries