Setting Variables

You now know just about everything there is to know about actually using WML variables, but one thing you don’t yet know is how to put useful values into the variables in the first place!

Actually, there are three ways. The most common is through the use of the various user interface elements, which are described in Chapter 4. Variables can also be set from WMLScript, as explained in Chapter 19. The third way is with the <setvar> element.

The <setvar> Element

This element must be placed within a task element (described in Chapter 3). Its purpose is to represent an assignment to a WML variable. This assignment takes place when the task is executed.

Attributes of the <setvar> element

name (required; variable string)

Specifies the name of the variable to be set

value (required; variable string)

Specifies the new value for the variable

For example, the element:

<setvar name="var" value="foo"/>

creates a variable called var, containing the string foo. If a variable called var already exists, this changes its value to the string foo.

Order of Operations in Setting Variables

Before executing one or more <setvar> elements within a task, the browser first expands all variables in all the name and value attributes. Note that this makes the order of the <setvar> elements within a task unimportant.

To illustrate this behavior, imagine that the variable x contains the value one, y contains two, and z contains three. Now suppose a task contains these three <setvar> elements:

<setvar name="x" ...

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.