What Is WMLScript?

WMLScript is loosely based on JavaScript but is much simpler in order to reduce the requirements on the device running the browser. (JavaScript can require quite large amounts of memory, and the interpreter to run it is complex.)

If you’ve ever done any programming in Java, JavaScript, C, C++, ECMAScript, or any of the large variety of other languages that borrow bits of syntax from each other, then a lot of the syntax of WMLScript should look familiar.

For example, a WMLScript function to calculate the factorial of a positive integer can be written as:

/* Calculate factorial of n. */
function factorial (n)
{
    var result = 1;

    for (var i=2; i<n; i++) {
        result *= n;
    }

    return result;
}

The curly braces, the for statement in the middle, and the return statement at the end are all similar to languages such as C, Java, JavaScript, or C++. The function line and the var line may be less familiar; they will be covered in later chapters.

WMLScript comments can have two forms: like in C, starting with /* and continuing until */ even across lines, and like in C++, starting with // and continuing until the end of the line:

/* C comments look like this. */

// C++ comments look like this.

/*
    C comments may span
    as many lines as you like.
*/

// C++ comments must have a
// new // at the start of every
// line of the comment.

Everything in WMLScript is case-sensitive, including function names, variable names, literal values, keywords, and everything else.

Spacing, however, is flexible. ...

Get Learning WML, and WMLScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.