HTTP.Query - Easy GET query string management
JSAN.use('HTTP.Query');
var query = new HTTP.Query(location.search);
query.set('from', 'me@example.com');
query.add('to', 'you@example.com');
query.add('to', 'him@example.com');
location.search = query.toString();
This class parses a GET query string, pulling out its argument keys and values and unescaping their encodings, and arranges them into an object. If a given query key is specified multiple times, then all of its values will be stored in an array for that key.
var query = new HTTP.Query;
var query = new HTTP.Query(queryString);
Returns a new HTTP.Query
object that has parsed the queryString
passed as the single argument. If no query argument is passed and HTTP.Query
is running in a Web browser, window.document.location.search
will be used, instead. To initialize an empty HTTP.Query
object, pass a null
or empty string to the constructor.
var value = query.get(key);
Gets the value stored for a given key in the table. If the key has multiple values, they will all be returned in an array object.
query.set(key, value);
Sets the value for a key. Any previous value or values for that key will be discarded.
query.unset(key);
Takes a single key argument and deletes that key from the query object, so that none of its values will be in the object any longer.
query.clear();
Clears the query object of all values.
query.add(key, value);
Adds a new value to the query object. This method is the best interface for adding mutiple values for a single key. Once a key has multiple values, those values will be returned from a call to get()
as an array.
query.act(function (key, value) {
document.write(key + ' => ' + value);
return true;
});
Pass a function to this method to have it iterate over all of the key/value pairs in the query object. Keys with multiple values will trigger the execution of the function multiple times: once for each value. The function should expect two arguments: a key and a value. Iteration terminates when the code reference returns false, so be sure to have it return a true value if you want it to iterate over every value in the query object.
alert(qry.toString());
location.search = query.toString('&');
alert(qry);
Overrides the default toString() method inherited from Object to return a properly-formatted and encoded GET query string. By default, it uses ";" as the query argument delimiter. Pass in "&" as the single argument to toString() for it to be used as the argument delimiter. Of course, when the HTTP.Query
object itself is used in a string context, thus implicitly calling toString() ";" will be the delimiter; call toString("&") explicitly to use "&" as the delimiter.
David Wheeler <david@kineticode.com>
Copyright 2005 by David Wheeler.
This program is free software; you can redistribute it and/or modify it under the terms of the Perl Artistic License or the GNU GPL.
See http://www.perl.com/perl/misc/Artistic.html and http://www.gnu.org/copyleft/gpl.html.