Many on-line programs, from search engines like google to RSS readers and aggregators, including Yahoo! Pipes as well as in programming on-offline mashups use URL query strings and parameters.
This post looks at:
A query string is only an item of text which describes a structured query, as a plain text string, which may be passed to some other service, program, or web site. It may seem like the following:
In the above mentioned example, designed to be used with URL query strings, there’s merely just one parameter. It’s snipped from your conventional Google URL query string:
More parameters could be added to the URL query string by dividing them along with the ‘&’ indication. The objective for the query string is a URL, in this case:
Yet, script empowered pages may also be utilized to good effect, enabling a particular amount of action.
as a way to pull the parameters, there are two operations to do on the URL query string:
var url = window.place.toString();
This code only returns a string encoding of the URL. Next, a straightforward call to .schism will empower the script to get everything after the ‘?’ as an array:
var query_string = url.split(“?”);
This leaves the root (up to the ?) The .schism function may subsequently be utilized again to deliver an array of tuples:
var params = query_string.schism(“&”);
Now, all of the params might be pulled with a straightforward .carve on the applicable component in the array:
var param_item = params.split(“=”);
This might be put in a loop, and then each item appraised for first the name, kept in param_item and then the value, kept in patam_item. Yet, it might be a lot easier to keep these as an associative array, by looping through the params array and assigning them thus:
param_list[param_item] = unescape(param_item);
The unescape call was added to remove URL escaping in the query string. By putting the values within an associative array, the programmer is then free to pull known values by name:
document.write(“q = ” + param_list[“q”]);
Using the aforementioned techniques, the programmer ought to be capable of assemble URL query strings to be used with any online service which takes them.