URLSearchParams(), get(), getAll(), append(), set(), toString()
new URLSearchParams(init)
URLSearchParams.prototype.get(name)
URLSearchParams.prototype.getAll(name)
URLSearchParams.prototype.append(name, value)
URLSearchParams.prototype.set(name, value)
URLSearchParams.prototype.toString()Parse and serialize query parameters, including repeated keys and plus-encoded spaces.
| Parameter | Description |
|---|---|
init | A query string without or with a leading ?, an iterable of name/value pairs, or a record of string values. |
name, value | The parameter name and value. get returns the first match; getAll returns every match. |
Returns: get returns a string or null. getAll returns an array of strings. toString returns the encoded query without ?.
const params = new URLSearchParams("q=a&q=b&q=c");
console.log(params.get("q"));
console.log(JSON.stringify(params.getAll("q")));
params.append("q", "d");
console.log(params.toString());
const plus = new URLSearchParams("q=a+b");
console.log(JSON.stringify(plus.get("q")));
plus.set("q", "a b");
console.log(plus.toString());
const encoded = new URLSearchParams();
encoded.set("q", "a b+c");
console.log(encoded.toString());
console.log(new URLSearchParams({ a: "1", b: "2" }).toString());
console.log(new URLSearchParams([["a", "1"], ["a", "2"]]).toString());
Expected output
a
["a","b","c"]
q=a&q=b&q=c&q=d
"a b"
q=a+b
q=a+b%2Bc
a=1&b=2
a=1&a=2
Related: URL