toQueryString
1.6 modified

toQueryString() -> String

Turns a hash into its URL-encoded query string representation.

This is a form of serialization, and is mostly useful to provide complex parameter sets for stuff such as objects in the Ajax namespace (e.g. Ajax.Request).

Undefined-value pairs will be serialized as if empty-valued. Array-valued pairs will get serialized with one name/value pair per array element. All values get URI-encoded using JavaScript's native encodeURIComponent function.

The order of pairs in the serialized form is not guaranteed (and mostly irrelevant anyway), except for array-based parts, which are serialized in array order.

Examples

$H({ action: 'ship', order_id: 123, fees: ['f1', 'f2'], 'label': 'a demo' }).toQueryString()
// -> 'action=ship=123=f1=f2=a%20demo'

// an empty hash is an empty query string:
$H().toQueryString()
// -> ''

Using Hash.toQueryString(obj) as a class method has been deprecated. Use it as an instance method only, or use Object.toQueryString instead.