Optional
init: string | URLSearchParams | Record<string, string | readonly string[]> | Iterable<[string, string]> | readonly [string, string][]Returns an ES6 Iterator
over each of the name-value pairs in the query.
Each item of the iterator is a JavaScript Array
. The first item of the Array
is the name
, the second item of the Array
is the value
.
Alias for urlSearchParams[@@iterator]()
.
Iterates over each name-value pair in the query and invokes the given function.
const myURL = new URL('https://example.org/?a=b&c=d');
myURL.searchParams.forEach((value, name, searchParams) => {
console.log(name, value, myURL.searchParams === searchParams);
});
// Prints:
// a b true
// c d true
Optional
thisArg: TThisTo be used as this
value for when fn
is called
Returns an ES6 Iterator
over the names of each name-value pair.
const params = new URLSearchParams('foo=bar&foo=baz');
for (const name of params.keys()) {
console.log(name);
}
// Prints:
// foo
// foo
Sets the value in the URLSearchParams
object associated with name
tovalue
. If there are any pre-existing name-value pairs whose names are name
,
set the first such pair's value to value
and remove all others. If not,
append the name-value pair to the query string.
const params = new URLSearchParams();
params.append('foo', 'bar');
params.append('foo', 'baz');
params.append('abc', 'def');
console.log(params.toString());
// Prints foo=bar&foo=baz&abc=def
params.set('foo', 'def');
params.set('xyz', 'opq');
console.log(params.toString());
// Prints foo=def&abc=def&xyz=opq
Sort all existing name-value pairs in-place by their names. Sorting is done with a stable sorting algorithm, so relative order between name-value pairs with the same name is preserved.
This method can be used, in particular, to increase cache hits.
const params = new URLSearchParams('query[]=abc&type=search&query[]=123');
params.sort();
console.log(params.toString());
// Prints query%5B%5D=abc&query%5B%5D=123&type=search
v7.7.0, v6.13.0
Returns an ES6 Iterator
over the values of each name-value pair.
The
URLSearchParams
API provides read and write access to the query of aURL
. TheURLSearchParams
class can also be used standalone with one of the four following constructors. TheURLSearchParams
class is also available on the global object.The WHATWG
URLSearchParams
interface and thequerystring
module have similar purpose, but the purpose of thequerystring
module is more general, as it allows the customization of delimiter characters (&
and=
). On the other hand, this API is designed purely for URL query strings.Since
v7.5.0, v6.13.0