20.1.1. Using the <setvar/> Tag
Setting a value to a variable using the <setvar/> tag is simple. The <setvar/> tag has two attributes, name and value. The name attribute defines the variable name and the value attribute defines the variable value. The following WML code assigns the value WML Tutorial to a variable called var1:
<setvar name="var1" value="WML Tutorial"/>
The <setvar/> tag should be enclosed in the following tags: <go></go>, <prev></prev>, <refresh></refresh>. For example, let's say you want to assign a value to a variable when a user follows an anchor link to a certain card, you should write something like this:
<anchor>
<go
href="tutorial_2.wml">
<setvar
name="last_tutorial" value="WML
Tutorial"/>
</go>
Go to next
tutorial
</anchor>
If you want to assign a value to a variable when a user clicks an anchor link but you do not want the WAP browser to leave the current card, you need to use the <refresh> tag instead of the <go> or <prev> tag. Here is a WML example that demonstrates how to use the <refresh> tag:
<anchor>
<refresh>
<setvar
name="last_tutorial" value="WML
Tutorial"/>
</refresh>
Refresh
variable
</anchor>
If you click the above anchor link, the value of the variable last_tutorial will be set to WML Tutorial. If the variable last_tutorial is shown in the current card, the WAP browser will refresh the card so that the updated value of last_tutorial will be displayed on the screen. (We will describe how to show a variable's value in a card in the "Referring to the Value of a Variable in WML" section.)
To initialize a variable without involving any user actions, use the <setvar> element with an event such as onenterforward and onenterbackward. The following WML example demonstrates how to initialize a variable when a card is loaded:
<card
id="card1">
<onevent
type="onenterforward">
<refresh>
<setvar
name="last_tutorial" value="WML
Tutorial"/>
</refresh>
</onevent>
...
</card>
Previous Page | Page 40 of 50 | Next Page |
- 1. WML (Wireless Markup Language) Introduction
- 2. WML Deck and Card
- 3. WML Document Structure
- 4. WML Generic Metadata: <meta>
- 5. Comments in WML
- 6. Line Breaking in WML
- 7. Font Size and Style in WML
- 8. WML Preformatted Text: <pre>
- 9. WML Images
- 10. WML Tables
- 11. WML Anchor Links
- 12. Programming Softkeys of Mobile Phones and the <do> Element
- 13. WML Template and Menu
- 14. WML Events and the <onevent> Tag
- 15. Timer and the ontimer Event
- 16. WML Event: onenterbackward
- 17. WML Event: onenterforward
- 18. WML Selection Lists and the onpick Event
- 19. WML Input Fields
- 20. WML Variables
- 21. Submitting Form Data to the Server in WML
- 22. Clearing a Saved Form in WML