Javascript URLSearchParams
URLSearchParams is an JavaScript Object that handle the search/query parameters in your URL. Details can be find in https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
Usage
Assume you have the query/search part of the URL. You can create an URLSearchParams object by:
var paramsString = "q=URLUtils.searchParams&topic=api"; var searchParams = new URLSearchParams(paramsString); //Iterate the search parameters. for (let p of searchParams) { console.log(p); } searchParams.has("topic") === true; // true searchParams.get("topic") === "api"; // true
Note that DO NOT USE THE WHOLE URL as the parameter! It will not work! Use the search/query part string only! If the string have a leading ?, URLSearchParams will strip it on create.
Member Methods
Most of the time we use get, has, for loop them through. Here is a list of URLSearchParams methods from https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
URLSearchParams.append() Appends a specified key/value pair as a new search parameter. URLSearchParams.delete() Deletes the given search parameter, and its associated value, from the list of all search parameters. URLSearchParams.entries() Returns an iterator allowing iteration through all key/value pairs contained in this object. URLSearchParams.forEach() Allows iteration through all values contained in this object via a callback function. URLSearchParams.get() Returns the first value associated with the given search parameter. URLSearchParams.getAll() Returns all the values associated with a given search parameter. URLSearchParams.has() Returns a Boolean indicating if such a given parameter exists. URLSearchParams.keys() Returns an iterator allowing iteration through all keys of the key/value pairs contained in this object. URLSearchParams.set() Sets the value associated with a given search parameter to the given value. If there are several values, the others are deleted. URLSearchParams.sort() Sorts all key/value pairs, if any, by their keys. URLSearchParams.toString() Returns a string containing a query string suitable for use in a URL. URLSearchParams.values() Returns an iterator allowing iteration through all values of the key/value pairs contained in this object.