Gets the URLSearchParams
object representing the query parameters of the URL. This property is read-only but the URLSearchParams
object it provides can be used to mutate the URL instance; to replace the entirety of query parameters of the URL, use the search setter. See URLSearchParams
documentation for details.
Use care when using .searchParams
to modify the URL
because, per the WHATWG specification, the URLSearchParams
object uses different rules to determine which characters to percent-encode. For instance, the URL
object will not percent encode the ASCII tilde (~
) character, while URLSearchParams
will always encode it:
const myURL = new URL('https://example.org/abc?foo=~bar');
console.log(myURL.search); // prints ?foo=~bar
// Modify the URL via searchParams...
myURL.searchParams.sort();
console.log(myURL.search); // prints ?foo=%7Ebar