Calling a Function
from a WML Card
URLs
are used to call WMLScript functions from a WML card. The URL
specifies the location of the WMLScript file and the name of the
function to be called. It should be in the form:
http://domain_name/path/wmlscript_filename#function_name(argument1,
argument2, ...)
The
URL can be in absolute or relative form.
The
following example demonstrates how to use anchor links to call
WMLScript functions from a WML card. Suppose we have a WMLScript file
called wmlscript.wmls, which contains the following script:
extern
function wmlscript_function() { WMLBrowser.setVar("message",
"Welcome to our WMLScript tutorial"); }
To
call wmlscript_function() from a WML card, create an anchor
link using the following WML markup. The script will be executed if
you click the anchor link.
<a
href="wmlscript.wmls#wmlscript_function()">Execute
Script</a>
Or
<anchor> <go
href="wmlscript.wmls#wmlscript_function()"/> Execute
Script </anchor>
Another
way to call wmlscript_function()
in a WML card is to place the URL in a WML event handler. The script
will be executed when the WML event occurs. For example, the
following markup instructs the WAP browser to call
wmlscript_function()
when the ontimer event
is triggered:
<onevent
type="ontimer"> <go
href="wmlscript.wmls#wmlscript_function()"/> </onevent> <timer
value="50"/>
Four
event types are supported in WML. They are ontimer,
onenterbackward,
onenterforward and
onpick. Details about
them can be found in the "WML
Events and the <onevent> Tag"
section of our WML
tutorial.
Passing
Arguments to Functions
The
wmlscript_function() function
above does not take any arguments. Now let's see how to pass arguments
to functions. Suppose there is another function called addition()
that takes two numbers as its arguments:
extern
function addition(number1, number2) { WMLBrowser.setVar("message",
number1 + number2); }
To
call addition() from a
WML card, we need to include two arguments in the URL. For example,
to pass two integers 10 and 11 to addition(),
we can make use of the following WML markup:
<a
href="wmlscript.wmls#addition(10, 11)">Execute
Script</a>
In
some situations, what you want to pass into a WMLScript function is
not a fixed value but is a WML variable. The following WML
markup demonstrates how to pass two WML variables, wmlVar1 and
wmlVar2, to the addition()
function:
<card
id="card1" title="WMLScript Tutorial"> <onevent
type="onenterforward"> <refresh> <setvar
name="wmlVar1" value="10"/> <setvar
name="wmlVar2"
value="11"/> </refresh> </onevent>
<p> <a
href="wmlscript.wmls#addition($(wmlVar1), $(wmlVar2))">Execute
Script</a> </p>
... </card>
When
the WAP browser comes across the terms $(wmlVar1) and $(wmlVar2), it
will substitute them with their stored value. So, the WML
markup:
<a
href="wmlscript.wmls#addition($(wmlVar1), $(wmlVar2))">Execute
Script</a>
will
become:
<a
href="wmlscript.wmls#addition(10, 11)">Execute
Script</a>
Note
that if the value of wmlVar1 and wmlVar2 contains
non-numeric characters, $(wmlVar1) and $(wmlVar2) have to be enclosed
in quotes so that the values are passed into the function as string
literals, like this:
<a
href="wmlscript.wmls#addition('$(wmlVar1)',
'$(wmlVar2)')">Execute Script</a>
Suppose
the values of wmlVar1 and
wmlVar2 are still 10
and 11 respectively. After substitution, the above WML markup
will become:
<a
href="wmlscript.wmls#addition('10', '11')">Execute
Script</a>
|
Feedback Form (ExpandCollapse)
|
|