9. WMLScript Variables
Vs WML Variables
Note
that WMLScript variables and WML variables are not the same thing.
WMLScript variables are those you see in the last few sections. They
are declared using the var
keyword and they can only be used inside WMLScript functions.
WML
variables are those variables whose values can be set and read using
the WML markup language alone. For example, we can use the WML tag
<setvar> to set the value of a WML variable without involving
WMLScript. Further details about WML variables can be found in the
"WML Variables" section
of our WML tutorial.
WMLScript
provides two functions in its WMLBrowser standard library for mobile
Internet application developers to manipulate WML variables.
They are setVar() and
getVar().
9.1. Using WMLScript to
Set the Value of a WML Variable
WMLScript's
setVar() function is
used to set the value of a WML variable. It should be used in
the following form:
WMLBrowser.setVar(variable_name,
value);
variable_name
is the name of the WML variable you want to assign a value to, and
value is the value you want to assign to variable_name.
WMLScript requires value
to be legal XML CDATA.
The
setVar() function
returns:
true
if the function call succeeds
false
if value cannot be assigned to variable_name
invalid
if variable_name or value
is of the wrong syntax, e.g. variable_name contains
forbidden characters (see "Setting
Variable Values in WML" of our WML
tutorial to learn what characters are allowed in WML variable
names)
Here
is an example demonstrating how to use the setVar()
function:
WMLBrowser.setVar("message",
"Hello World. Welcome to our WMLScript tutorial.");
The
above line of script assigns the value "Hello World. Welcome to
our WMLScript tutorial." to the WML variable message.
9.2. Using WMLScript to
Obtain the Value of a WML Variable
WMLScript's
getVar() function helps you
obtain the value of a WML variable. It is used in the following form:
WMLBrowser.getVar(variable_name);
variable_name
is the name of a WML variable.
The
getVar() function
returns:
a
string containing the value stored in the WML variable variable_name
if the function call succeeds
an
empty string if the WML variable variable_name does not exist
invalid
if variable_name is of the wrong syntax, e.g. variable_name
contains characters that are not allowed to appear in WML variable
names.
Here
is an example demonstrating how to use the getVar()
function:
var
wmlscript_variable = WMLBrowser.getVar("message");
When
the WMLScript interpreter encounters the above line of script, the
value of the WML variable message
is assigned to the WMLScript variable wmlscript_variable.
Feedback Form (ExpandCollapse)
|
|