class URLSearchParams extends Object with StObject with Iterable[Tuple2[String, String]]
The URLSearchParams
API provides read and write access to the query of aURL
. The URLSearchParams
class can also be used standalone with one of the
four following constructors.
The URLSearchParams
class is also available on the global object.
The WHATWG URLSearchParams
interface and the querystring
module have
similar purpose, but the purpose of the querystring
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.
js
const myURL = new URL('https://example.org/?abc=123');
console.log(myURL.searchParams.get('abc'));
// Prints 123
myURL.searchParams.append('abc', 'xyz'); console.log(myURL.href); // Prints https://example.org/?abc=123&abc=xyz
myURL.searchParams.delete('abc'); myURL.searchParams.set('a', 'b'); console.log(myURL.href); // Prints https://example.org/?a=b
const newSearchParams = new URLSearchParams(myURL.searchParams); // The above is equivalent to // const newSearchParams = new URLSearchParams(myURL.search);
newSearchParams.append('a', 'c'); console.log(myURL.href); // Prints https://example.org/?a=b console.log(newSearchParams.toString()); // Prints a=b&a=c
// newSearchParams.toString() is implicitly called
myURL.search = newSearchParams;
console.log(myURL.href);
// Prints https://example.org/?a=b&a=c
newSearchParams.delete('a');
console.log(myURL.href);
// Prints https://example.org/?a=b&a=c
- Annotations
- @JSType() @JSImport("url", "URLSearchParams") @native()
- Since
v7.5.0, v6.13.0
- Alphabetic
- By Inheritance
- URLSearchParams
- Iterable
- StObject
- Object
- Any
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def append(name: String, value: String): Unit
Append a new name-value pair to the query string.
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def delete(name: String): Unit
Remove all name-value pairs whose name is
name
. - def entries(): IterableIterator[Tuple2[String, String]]
Returns an ES6
Iterator
over each of the name-value pairs in the query.Returns an ES6
Iterator
over each of the name-value pairs in the query. Each item of the iterator is a JavaScriptArray
. The first item of theArray
is thename
, the second item of theArray
is thevalue
.Alias for
urlSearchParams[@@iterator]()
. - final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def forEach[TThis](callback: ThisFunction3[TThis, String, String, URLSearchParams.this.type, Unit], thisArg: TThis): Unit
- def forEach[TThis](callback: ThisFunction3[TThis, String, String, URLSearchParams.this.type, Unit]): Unit
Iterates over each name-value pair in the query and invokes the given function.
Iterates over each name-value pair in the query and invokes the given function.
js 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
- def get(name: String): |[String, Null]
Returns the value of the first name-value pair whose name is
name
.Returns the value of the first name-value pair whose name is
name
. If there are no such pairs,null
is returned.- returns
or
null
if there is no name-value pair with the givenname
.
- def getAll(name: String): Array[String]
Returns the values of all name-value pairs whose name is
name
.Returns the values of all name-value pairs whose name is
name
. If there are no such pairs, an empty array is returned. - final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def has(name: String): Boolean
Returns
true
if there is at least one name-value pair whose name isname
. - def hasOwnProperty(v: String): Boolean
- Definition Classes
- Object
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isPrototypeOf(v: Object): Boolean
- Definition Classes
- Object
- val iterator: Function0[Iterator[Tuple2[String, String]]]
- Definition Classes
- Iterable
- Annotations
- @JSName(js.Symbol.iterator)
- var iterator_URLSearchParams: Function0[IterableIterator[Tuple2[String, String]]]
- Annotations
- @JSName(js.Symbol.iterator)
- def keys(): IterableIterator[String]
Returns an ES6
Iterator
over the names of each name-value pair.Returns an ES6
Iterator
over the names of each name-value pair.js const params = new URLSearchParams('foo=bar&foo=baz'); for (const name of params.keys()) { console.log(name); } // Prints: // foo // foo
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def propertyIsEnumerable(v: String): Boolean
- Definition Classes
- Object
- def set(name: String, value: String): Unit
Sets the value in the
URLSearchParams
object associated withname
tovalue
.Sets the value in the
URLSearchParams
object associated withname
tovalue
. If there are any pre-existing name-value pairs whose names arename
, set the first such pair's value tovalue
and remove all others. If not, append the name-value pair to the query string.js 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
- def sort(): Unit
Sort all existing name-value pairs in-place by their names.
Sort all existing name-value pairs in-place by their names. Sorting is done with a [stable sorting algorithm](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability), so relative order between name-value pairs with the same name is preserved.
This method can be used, in particular, to increase cache hits.
js 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
- Since
v7.7.0, v6.13.0
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toLocaleString(): String
- Definition Classes
- Object
- def toString(): String
- Definition Classes
- AnyRef → Any
- def valueOf(): Any
- Definition Classes
- Object
- def values(): IterableIterator[String]
Returns an ES6
Iterator
over the values of each name-value pair. - final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated