Adding Client-Side Logic To WAP Using WMLScript
WMLScript is the WAP corollary to the JavaScript scripting language that was popularized by Netscape Communications. Standardization efforts by Netscape helped produce the ECMAScript standard, a standard that WMLScript was based on. While JavaScript has since been coopted by server tool vendors (including Netscape and Microsoft), WMLScript is a client-only scripting platform used in combination with WML to provide client side procedural logic. Like WML, WMLScript is compiled via a WAP gateway into binary form to provide intelligence to mobile clients. In this brief tutorial, we’ll discuss what WMLScript is and how to use it. For more information on WMLScript, visit the WAP Forum.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.
var x;
)
Data Types
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:
Boolean
Integer
Floating-point
String
Invalid
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.Operators
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
The operators and expressions supported by WMLScript are virtually identical to those of the JavaScript programming language so we will not discuss them here. Java does support a number of control statements for handling branching within programs. These include the if-else, for loop, while loop, break, and continue statements.Functions
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 ;
The
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; return time; };
The above example simply takes two input variables,
distance
and 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");
The WMLScript Standard Libraries
While WMLScript does not support the creation of new objects via object-oriented programming, it does provide six "pre-built" libraries that aid in the handling of many common tasks. These libraries (with a brief description of each) include:
- Lang - This library contains a set of functions that are closely related to the WMLScript language core. Included in this library are functions for data type manipulation, absolute value calculations, and random number generation.
- Float - The Float library is optional and is only supported on those clients who have floating-point capabilities. Typical functions provided by this library include sqrt(), round(), and pow().
- String - The String library contains a set of functions for performing string operations. Some of the functions included in this library are length(), charAt(), find(), replace(), and trim().
- URL - This library contains a set of functions for handling both absolute URLs and relative URLs. Typical functions include getPath(), getReferer(), and getHost().
- WMLBrowser - This library contains functions by which WMLScript can access the associated WML context. These functions must not have any side effects and must return invalid in cases where the system does not support WMLBrowser and where the interpreter is not invoked by the WML Browser. Commonly used functions in this library include go(), prev(), next(), getCurrentCard(), and refresh().
- Dialogs - This library contains a set of typical user interface functions including prompt(), confirm(), and alert().
Example: Validating User Input Via WMLScript
In the following example, we will build a simple WML card that asks the user to input a social security number (an identification number used by the U.S. Social Security Administration). We will then use WMLScript to verify that the user’s input was formatted correctly. Following this verification, we’ll alert the user via WMLScript to let them know whether their number was accepted or not. This example, though simple, represents a typical usage of WMLScript on a client.To build this example, we create a normal WML file containing two cards: an input
card and a results
card (see Listing 1 below). Accepting the input will result in ourvalidateSSN()
function being called. Note that this function is stored in a separate .wmls file (WMLScriptExample.wmls) and is declared within that file using the extern
keyword. extern
allows a function to be called by other functions or WML events that exist outside of the function’s source file. To keep a function "private", simply declare the function without using the extern
keyword.
Listing 1 - WMLScriptExample.wml
Listing 2 - WMLScriptExample.wmls
extern function validateSSN(SSN)
{
if (String.length(SSN) != 9)
{
WMLBrowser.setVar("SSN", "Error: String must be 9 digits long.");
}
WMLBrowser.go("WMLScriptExample.wml#Results");
};

Figure 1 
Figure 2
For more information on the WML code above, see our WML tutorial. The WMLScript function shown in Listing 2 makes use of two of the standard WMLScript libraries: WMLBrowser and String. The WMLBrowser.setVar()
function sets the value of a WML variable while the WMLBrowser.go()
function redirects execution of the script to a card within a WML source file.
No comments:
Post a Comment