The Query module provides methods to parse and build query strings and may optionally parse
location.search
, exposing the parsed value through the UsefulJS.Query.parameters
object.
The following might be the query string supplied to an online pizza ordering application:
?pickup&size=14&topping=pepperoni&topping=extra+cheese&coupon=0144732
The query part of the URL is denoted by '?'. The invidual items in the query string are separated by '&' and the keys and values are separated by '='. Certain characters need to be escaped (note "extra+cheese", where '+' is the escaped value for ' '). The general form of an escaped character is "%XY", where "XY" is the hexadecimal representation of the character code value. A key with no associated value is a simple boolean flag.
UsefulJS.Query.parse
method will turn the above string into the following value:
{ pickup : true, size : 14, topping : ["pepperoni", "extra cheese"], coupon : "0144732" }
Parses a query string into a JavaScript Object
UsefulJS.Query.parse(s)
s
StringReturns: Object The parsed query parameters.
The input string is parsed according to the following rules:
true
String(parseInt(v, 10)) === v)
), the output property is a
Number
String
Array
.Used to build up a query string one key-value pair at a time
UsefulJS.Query.append(s, n, v)
s
Stringn
Stringv
Boolean|String|Number|ArrayReturns: String The modified query string.
The value to add may be one of the following types:
The appended key-value pair is correctly URI-encoded.
// Outputs size=14&topping=pepperoni&topping=extra%20cheese var q = UsefulJS.Query, queryStr = q.append(q.append("", "size", 14), "topping", ["pepperoni", "extra cheese"]);
Convenience method to assemble a query string in one go.
UsefulJS.Query.build(params)
params
ObjectReturns: String The assembled query string.
Each value in the params Object must be Boolean true
, a String, a Number
or an Array as with the v
parameter to the append
method. The
return value will be correctly URI-encoded.
// Outputs pickup&size=14&topping=pepperoni&topping=extra%20cheese var q = UsefulJS.Query, params = { pickup : true, size : 14, topping : ["pepperoni", "extra cheese"] }, queryStr = q.build(params);
This property is an interface to the query parameters for the current page. It provides three methods to manage the query string.
Parses location.search
.
UsefulJS.Query.parameters.parse()
Gets the value of a parameter.
UsefulJS.Query.parameters.get(prop, def)
prop
Stringdef
Anyprop
is not found in the query parametersReturns: Any The value associated with the parameter or def
if the
property is not found.
Clones the parsed query parameters for the page and returns them. Modifications to the return value will not affect the original parameters object.
UsefulJS.Query.parameters.all()
// Are we running in debug mode? var qparams = UsefulJS.Query.parameters, logLevel = 0; qparams.parse(); if (qparams.get("debug", false)) { logLevel = qparams.get("verbose", 1); }
The fixes for the Query module are defined in the _query namespace of the fix options.
The parse
fix parses any query string supplied to the current page. It is enabled by
default. The property set in the return value is a boolean value called "parsed".