WMLScript is to WML what JavaScript is to HTML; it is a
scripting language, which (like JavaScript) is based on ECMAScript,
although it is not fully compliant with ECMAScript. It includes all
of the usual procedural constructs that you would expect in a
scripting language, such as loops, conditions, and so on.
For the purposes of efficiency, it is compiled on the server
into byte code, which is then executed on the mobile device in the
VM. Each compilation unit is referenced by a URL, with externalized
functions within the compilation unit referenced by fragment
anchors.
Functions are also built into WMLScript, and additional
functions are available in the standard WMLScript libraries, which
include functions for language, string manipulation, URL handling,
an interface to the browser itself, floating points, and dialogs.
There is also a cryptography library, but it only contains a
function for signing plaintext at this time.
WMLScript is weakly typed, meaning that type checking is not
enforced and a variable's type may change during its lifetime.
Variables must be declared, but the data type isn't. The types
supported are Boolean, integer (32 bit), floating-point (32 bit
single precision), string, and invalid. Automatic conversion
between data types is supported wherever possible.
The typical operations that you would expect in any scripting
language are supported. This includes the assignment operators,
arithmetic operators, logical operations, string manipulation and
comparison operators. Arrays are not directly supported, but the
string functions support indexing. Two operators, typeof and
isvalid are used for type checking.
Functions are declared with a name, the parameters that the
function accepts, and a block statement that contains the code.
Parameters are passed by value and are treated as local variables.
All functions always return a value, and the default value is an
empty string. Function declarations cannot be nested. Local
functions, that is, functions within the same compilation unit, are
referenced directly by name, whereas external functions are called
using a URL and fragment anchor. Standard library functions are
called using the library name and function name separated by a
period. The code snippet in the next section contains an example of
calling a standard function in this way.
Conditional branching is limited to if and if else statements.
Both while and for statements are supported for looping, with break
and continue statements to control program flow in loops.
WMLScript can be used to add a level of intelligence and
processing capability to WML pages. Used appropriately, it can help
to reduce the amount of network transmission in an application by
performing some input validation on the mobile device. It is
typically invoked in response to events on the mobile device.
WML variables can be used in WMLScript.
What Does WMLScript Look Like?
The snippet of WMLScript below shows a very simple function that
maps a printer's name to a printer code.
/*
* Given a printers name, put the printer's
* code in the variable returnPrinter.
*@param printer the variable name in which to store the
printer code
*@param printerName the printer's name
*/
extern function getPrinter(printer, printerName)
{
var returnPrinter ="Printer Unknown ...";
if ("First Floor Laser 1" == printerName) {
returnPrinter = "FFLPRNT01";
}
else if ("First Floor Laser 2" == printerName) {
returnPrinter = "FFLPRNT02";
}
else if ("Second Floor Laser 1" == printerName) {
returnPrinter = "SFLPRNT01";
}
else if ("Colour Printer" == printerName) {
returnPrinter = "CLRPRNT";
}
else if ("CEO's Printer" == printerName) {
returnPrinter = "CEOPRNT";
}
WMLBrowser.setVar(printer, returnPrinter);
/*
* Make sure the browser updates its
display.
*/
WMLBrowser.refresh();
};
The code shows an example of passing parameters into a function
and setting a variable. if else statements are used to perform a
very simple translation. Note the use of the refresh function from
the WMLBrowser standard library to update the display at the end of
the function.