15.4. Getting and Setting WML Variable Values: getVar() and setVar() Functions

The getVar() and setVar() functions of the WMLScript standard library are very frequently used in WMLScript code. The getVar() function helps us get the value of a WML variable and the setVar() function helps us set the value of a WML variable.

We have mentioned about how to use these two functions earlier in the "WMLScript Variables Vs WML Variables" section of this tutorial. We will just describe them briefly here for completeness.

This is the syntax of getVar():


WMLBrowser.getVar(variable_name);


The getVar() function returns a string containing the value of the WML variable variable_name. If the WML variable variable_name does not exist, an empty string is returned. If variable_name is of the wrong syntax (e.g. it contains characters that cannot appear in WML variable names), invalid is returned.

This is the syntax of setVar():


WMLBrowser.setVar(variable_name, value);


The setVar() function sets value to the WML variable variable_name. WMLScript requires value to be legal XML CDATA.

The setVar() function returns true if everything works fine, false if value cannot be assigned to variable_name, and invalid if variable_name or value is of the wrong syntax (for example, variable_name contains characters that cannot appear in WML variable names).

The following WMLScript example illustrates the use of getVar() and setVar():


function example_func()
{
  WMLBrowser.setVar("message", "Welcome to our WMLScript tutorial.");
  var message = WMLBrowser.getVar("message");
}


If the above function is called, the value "Welcome to our WMLScript tutorial." will be set to the WML variable message. Then the value of message will be assigned to the WMLScript variable message.


Previous Page Page 34 of 71 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