instance method Hash#toQueryString

View source on GitHub →

Hash#toQueryString() → String

Returns a URL-encoded string containing the hash's contents as query parameters according to the following rules:

  • An undefined value results a parameter with no value portion at all (simply the key name, no equal sign).
  • A null value results a parameter with a blank value (the key followed by an equal sign and nothing else).
  • A boolean value results a parameter with the value "true" or "false".
  • An Array value results in a parameter for each array element, in array order, each using the same key.
  • All keys and values are URI-encoded using JavaScript's native encodeURIComponent function.

The order of pairs in the string is not guaranteed, other than the order of array values described above.

$H({action: 'ship',
    order_id: 123,
    fees: ['f1', 'f2']
// -> "action=ship&order_id=123&fees=f1&fees=f2"
 $H({comment: '',
    'key with spaces': true,
    related_order: undefined,
    contents: null,
    'label': 'a demo'
// -> "comment=&key%20with%20spaces=true&related_order&contents=&label=a%20demo"
 // an empty hash is an empty query string:
// -> ""