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 |
- 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