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

Preferences - Do not show ads

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

Contents at a Glance
Print this Web Page | | Back to Top

Feedback Form (Expand)

What do you think about this web page?






WMLScript Tutorial Table of Contents WMLScript Tutorial Contents at a Glance Preferences - Change Color Scheme Preferences - Do Not Show Ads Previous Page Next Page