6.3. 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.
6.3.1. 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>
Previous Page | Page 10 of 71 | Next Page |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data