Entry
How do I parse the query string?
How do I parse the query string constructed by a GET FORM submission?
May 13th, 2002 02:45
Techek, Martin Honnen,
A FORM with METHOD="get" attaches the form data to the url as the so
called query string, for instance
http://www.geeks.com/addPerson?name=Kibo&home=http%3A%2F%2Fwww.kibo.com
This query string (starting with the quotation mark) is accessible in
client side javascript as the string
window.location.search
To parse the field names and field values into a handable object you
can use the following function
function parseQueryString (str) {
str = str ? str : location.search;
var query = str.charAt(0) == '?' ? str.substring(1) : str;
var args = new Object();
if (query) {
var fields = query.split('&');
for (var f = 0; f < fields.length; f++) {
var field = fields[f].split('=');
args[unescape(field[0].replace(/\+/g, ' '))] =
unescape(field[1].replace(/\+/g, ' '));
}
}
return args;
}
The following is an example how to use that function to list all data
passed to the page:
<SCRIPT>
var args = parseQueryString ();
for (var arg in args) {
document.write(arg + ': ' + args[arg] + '<BR>');
}
</SCRIPT>
(Article-searchwords : request_uri requeststring querystring )