toQueryString :: Hash

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&order_id=123&fees=f1&fees=f2&label=a%20demo'

$H().toQueryString()

// -> ''

Note

This method can be called in two ways: as an instance method (as in the above examples) or as a class method on Hash.

Hash.toQueryString({ foo:'bar' })

// -> 'foo=bar'

This way you can generate a query string from an object without converting it to a Hash instance, making it possible to serialize hashes that have keys corresponding to Enumerable method names.

Prototype API 1.5.0 - prototypejs.org