URLSearchParams
The interface defines some practical methods to process the query string of the URL.
method:
This interface does not inherit any attributes.
Insert a specified key / value pair as a new search parameter.
Delete the specified search parameter and its corresponding value from the search parameter list.
Returns an iterator
object that can traverse all key / value pairs.
Get the first value of the specified search parameter.
Get all the values of the specified search parameters, the return is an array.
Returns Boolean
whether the search parameter exists.
Returns iterator
all the key names of this object containing key / value pairs.
Set a new value for the search parameter. If there are multiple values, all other values will be deleted.
Sort by key name.
Returns a string composed of search parameters, which can be used directly on the URL.
Returns iterator
all values of this object containing key / value pairs.
Examples
var paramsString = "q=URLUtils.searchParams&topic=api"
var searchParams = new URLSearchParams(paramsString);
for (let p of searchParams) {
console.log(p); // [q, URLUtils.searchParams]、[topic, api]
}
searchParams.has("topic") === true; // true
searchParams.get("topic") === "api"; // true
searchParams.getAll("topic"); // ["api"]
searchParams.get("foo") === ""; // true
searchParams.append("topic", "webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=api&topic=webdev"
searchParams.set("topic", "More webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=More+webdev"
searchParams.delete("topic");
searchParams.toString(); // "q=URLUtils.searchParams"