20.4. Extracting Query Parameters of an URL
In this section, we will demonstrate how to extract query parameters of an URL using some of the functions that we learned before.
Before extracting query parameters, we have to obtain the query string of the URL first. This can be done using the getQuery() function that we learned in the last section.
After extracting the query string from the URL, you can use the elements() and elementAt() functions of the String standard library to extract the query parameters. For example, let's say the query string obtained is "q1=Hello&q2=Welcome%20to%20our%20WMLScript%20tutorial". We use the following example script to extract the parameters and assign their values to a variable named message, whose value can be displayed in a WML card if you want to do so:
var
queryString =
"q1=Hello&q2=Welcome%20to%20our%20WMLScript%20tutorial";
var
paraTemp = "";
var message = "";
for (var
i=0; i<String.elements(queryString, "&");
i++){
paraTemp = String.elementAt(queryString, i,
"&");
message += "The value of the " +
String.elementAt(paraTemp, 0, "=") + " parameter is '"
+ String.elementAt(paraTemp, 1, "=") + "'. ";
}
After executing the above script, the message variable contains the string value "The value of the q1 parameter is 'Hello'. The value of the q2 parameter is 'Welcome%20to%20our%20WMLScript%20tutorial'. ".
Here is a brief description of what we do in the above WMLScript example. First, we use the & character as the separator to divide the query string into two substrings "q1=Hello" and "q2=Welcome%20to%20our%20WMLScript%20tutorial".
Next, we divide each of the substrings into two parts using the = character as the separator. For example, we divide "q1=Hello" into "q1" and "Hello".
Finally, we output the parameter names and parameter values to the message variable.
As you can see above, the value the q2 parameter is in the escaped form. The unescapeString() function can help us unescape the hexadecimal sequence:
message = URL.unescapeString(message);
After executing the above line of script, the message variable contains the string value "The value of the q1 parameter is 'Hello'. The value of the q2 parameter is 'Welcome to our WMLScript tutorial'. ".
Previous Page | Page 65 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