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


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page