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


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