The var
statement is used to create a named variable. This refered to as
initializing the variable. At the same time you can also set a value
(including the empty string) for the variable, or, by default, allow
the variable to assume the value of the empty string "" and assign a
value at a later time. Indeed, after a variable has been created, you
can change its value whenever you wish and as many times as you wish.
A named variable is said to have a scope. By
scope, we mean where the named variable will be recognized by the
program. In WMLS, the scope of a variable is limited to inside the
function in which the var is declared. However, you can use the
WMLBrowser.setVar function to set a named variable with a value
that can be displayed in a .wml program.
The
name of the variable is a string that is composed of characters that
are legally recognized by WMLS. The name must be unique within the
function. Clearly you can not use the names of the WMLScript libray
functions, operators, or statements as a variable name since they are
considered reserved words.
<?xml
version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD
WML 1.1//EN"
"http://www.WAPforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1">
<p>
for
example
</p>
<do type="accept">
<go
href="ForExample.wmls#findfor()" />
</do>
</card>
<card id="card2">
<p>
$(str)
</p>
</card>
</wml>Code for ForExample.wml
extern function findfor()
{
var boo;
var hoo =
"";
var
myfavoritenum = 95;
var a=10.0;
var b=34.8*a;
var name =
"Fred";
for(var count=0; count<99; count++;)
var str = "My pet
jaguar is named Oztotl.";
WMLBrowser.setVar("str" str);
WMLBrowser.go("ForExample.wml#card2");
};
Code for ForExample.wmls