>The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import<a name="section2080512344162"></a>
<td class="cellrowborder" valign="top" width="48.41%" headers="mcps1.1.5.1.4 "><p id="p12478654172415"><a name="p12478654172415"></a><a name="p12478654172415"></a>Input parameter objects, which include the following:</p>
| init | string[][] \| Record<string, string> \| string \| URLSearchParams | No| Input parameter objects, which include the following: <br/>- **string[][]**: two-dimensional string array<br/>- **Record<string, string>**: list of objects<br/>- **string**: string<br/>- **URLSearchParams**: object|
```
var objectParams = new URLSearchParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]);
var objectParams1 = new URLSearchParams({"fod" : 1 , "bard" : 2});
var objectParams2 = new URLSearchParams('?fod=1&bard=2');
var urlObject = new URL('https://developer.mozilla.org/?fod=1&bard=2');
var params = new URLSearchParams(urlObject .search);
```
- Example
```
var objectParams = new URLSearchParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]);
var objectParams1 = new URLSearchParams({"fod" : 1 , "bard" : 2});
var objectParams2 = new URLSearchParams('?fod=1&bard=2');
var urlObject = new URL('https://developer.mozilla.org/?fod=1&bard=2');
var params = new URLSearchParams(urlObject .search);
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p69671631796"><a name="p69671631796"></a><a name="p69671631796"></a>Key of the key-value pair to append.</p>
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p19675312911"><a name="p19675312911"></a><a name="p19675312911"></a>Value of the key-value pair to append.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new URLSearchParams(urlObject.search.slice(1));
paramsObject.append('fod', 3);
```
### delete<a name="section25256353342"></a>
delete\(name: string\): void
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the key-value pair to append.|
| value | string | Yes| Value of the key-value pair to append.|
- Example
```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new URLSearchParams(urlObject.search.slice(1));
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p4652192063817"><a name="p4652192063817"></a><a name="p4652192063817"></a>Key of the key-value pairs to delete.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsobject = new URLSearchParams(urlObject.search.slice(1));
paramsobject.delete('foo');
```
### getAll<a name="section2178125553415"></a>
getAll\(name: string\): string\[\]
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the key-value pairs to delete.|
- Example
```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsobject = new URLSearchParams(urlObject.search.slice(1));
paramsobject.delete('foo');
```
### getAll
getAll(name: string): string[]
Obtains all the key-value pairs based on the specified key.
Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value, respectively.
<td class="cellrowborder" valign="top" width="57.230000000000004%" headers="mcps1.1.5.1.4 "><p id="p16631613131917"><a name="p16631613131917"></a><a name="p16631613131917"></a>Value to use when the callback is invoked.</p>
<td class="cellrowborder" valign="top" width="58.220000000000006%" headers="mcps1.2.5.1.4 "><p id="p17767351572"><a name="p17767351572"></a><a name="p17767351572"></a>Value that is currently traversed.</p>
<td class="cellrowborder" valign="top" width="58.220000000000006%" headers="mcps1.2.5.1.4 "><p id="p186310399556"><a name="p186310399556"></a><a name="p186310399556"></a>Key that is currently traversed.</p>
Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value respectively.
- Return values
| Type| Description|
| -------- | -------- |
| IterableIterator<[string, string]> | An ES6 iterator.|
- Example
```
var searchParamsObject = new URLSearchParams("keyName1=valueName1&keyName2=valueName2");
for (var pair of searchParamsObject .entries()) { // Show keyName/valueName pairs
<td class="cellrowborder" valign="top" width="79.03999999999999%" headers="mcps1.1.3.1.2 "><p id="p136452242"><a name="p136452242"></a><a name="p136452242"></a>Returns the value of the first key-value pair if obtained.</p>
<td class="cellrowborder" valign="top" width="79.03999999999999%" headers="mcps1.1.3.1.2 "><p id="p7907183802419"><a name="p7907183802419"></a><a name="p7907183802419"></a>Returns <strong id="b1390712418471"><a name="b1390712418471"></a><a name="b1390712418471"></a>null</strong> if no value is obtained.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
var paramsOject = new URLSearchParams(document.location.search.substring(1));
var name = paramsOject.get("name"); // is the string "Jonathan"
var age = parseInt(paramsOject.get("age"), 10); // is the number 18
var address = paramsOject.get("address"); // null
```
### has<a name="section1385211103813"></a>
has\(name: string\): boolean
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain the value.|
- Return values
| Type| Description|
| -------- | -------- |
| string | Returns the value of the first key-value pair if obtained.|
| null | Returns null if no value is obtained.|
- Example
```
var paramsOject = new URLSearchParams(document.location.search.substring(1));
var name = paramsOject.get("name"); // is the string "Jonathan"
var age = parseInt(paramsOject.get("age"), 10); // is the number 18
var address = paramsOject.get("address"); // null
```
### has
has(name: string): boolean
Checks whether a key has a value.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to search for its value.|
- Return values
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the value exists; returns **false** otherwise.|
- Example
```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new URLSearchParams(urlObject.search.slice(1));
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p24691217436"><a name="p24691217436"></a><a name="p24691217436"></a>Key specified to search for its value.</p>
<td class="cellrowborder" valign="top" width="79.01%" headers="mcps1.1.3.1.2 "><p id="p5592172072510"><a name="p5592172072510"></a><a name="p5592172072510"></a>Returns <strong id="b1282341020222"><a name="b1282341020222"></a><a name="b1282341020222"></a>true</strong> if the value exists; returns <strong id="b54246565259"><a name="b54246565259"></a><a name="b54246565259"></a>false</strong> otherwise.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new URLSearchParams(urlObject.search.slice(1));
paramsObject.has('bard') === true;
```
### set<a name="section186091516203910"></a>
set\(name: string, value: string\): void
set(name: string, value: string): void
Sets the value for a key. If key-value pairs matching the specified key exist, the value of the first key-value pair will be set to the specified value and other key-value pairs will be deleted. Otherwise, the key-value pair will be appended to the query string.
<td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p053215214717"><a name="p053215214717"></a><a name="p053215214717"></a>Key of the value to set.</p>
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new URLSearchParams(urlObject.search.slice(1));
paramsObject.set('baz', 3); // Add a third parameter.
```
### sort<a name="section102851552183915"></a>
sort\(\): void
Sorts all key-value pairs contained in this object based on the Unicode code points of the keys and returns **undefined**. This method uses a stable sorting algorithm, that is, the relative order between key-value pairs with equal keys is retained.
- Example
```
var searchParamsObject = new URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object
searchParamsObject.sort(); // Sort the key/value pairs
console.log(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=2&c=3&d=4
```
### keys<a name="section816918183413"></a>
keys\(\): IterableIterator<string\>
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the value to set.|
| value | string | Yes| Value to set.|
- Example
```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new URLSearchParams(urlObject.search.slice(1));
paramsObject.set('baz', 3); // Add a third parameter.
```
### sort
sort(): void
Obtains an ES6 iterator that contains the keys of all the key-value pairs.
- Return values
Sorts all key-value pairs contained in this object based on the Unicode code points of the keys and returns undefined.排序顺序是根据键的Unicode代码点。This method uses a stable sorting algorithm, that is, the relative order between key-value pairs with equal keys is retained.
<td class="cellrowborder" valign="top" width="67.45%" headers="mcps1.1.3.1.2 "><p id="p1128142484911"><a name="p1128142484911"></a><a name="p1128142484911"></a>ES6 iterator that contains the keys of all the key-value pairs.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
var searchParamsObject = new URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object
searchParamsObject.sort(); // Sort the key/value pairs
console.log(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=2&c=3&d=4
```
- Example
```
var searchParamsObject = new URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
for (var key of searchParamsObject .keys()) { // Output key-value pairs
console.log(key);
}
```
### keys
keys(): IterableIterator<string>
### values<a name="section0542174314413"></a>
values\(\): IterableIterator<string\>
Obtains an ES6 iterator that contains the keys of all the key-value pairs.
- Return values
| Type| Description|
| -------- | -------- |
| IterableIterator<string> | ES6 iterator that contains the keys of all the key-value pairs.|
- Example
```
var searchParamsObject = new URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
for (var key of searchParamsObject .keys()) { // Output key-value pairs
console.log(key);
}
```
### values
values(): IterableIterator<string>
Obtains an ES6 iterator that contains the values of all the key-value pairs.
- Return values
- Return values
| Type| Description|
| -------- | -------- |
| IterableIterator<string> | ES6 iterator that contains the values of all the key-value pairs.|
- Example
```
var searchParams = new URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value respectively.
- Return values
| Type| Description|
| -------- | -------- |
| IterableIterator<[string, string]> | An ES6 iterator.|
- Example
```
const paramsObject = new URLSearchParams('fod=bay&edg=bap');
for (const [name, value] of paramsObject) {
console.log(name, value);
}
```
### tostring
toString(): string
Obtains search parameters that are serialized as a string and, if necessary, percent-encodes the characters in the string.
- Return values
| Type| Description|
| -------- | -------- |
| string | String of serialized search parameters, which is percent-encoded if necessary.|
- Example
```
let url = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new URLSearchParams(url.search.slice(1));
<td class="cellrowborder" valign="top" width="63.49%" headers="mcps1.1.3.1.2 "><p id="p38375645210"><a name="p38375645210"></a><a name="p38375645210"></a>ES6 iterator that contains the values of all the key-value pairs.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
var searchParams = new URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value, respectively.
<td class="cellrowborder" valign="top" width="79.01%" headers="mcps1.1.3.1.2 "><p id="p516412017563"><a name="p516412017563"></a><a name="p516412017563"></a>String of serialized search parameters, which is percent-encoded if necessary.</p>
</td>
</tr>
</tbody>
</table>
- Example
```
let url = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new URLSearchParams(url.search.slice(1));
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p72131224114411"><aname="p72131224114411"></a><aname="p72131224114411"></a>Gets and sets a string that contains a harsh mark (#) followed by the fragment identifier of a URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p12865829133514"><aname="p12865829133514"></a><aname="p12865829133514"></a>Gets and sets the host information in a URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p737414373372"><aname="p737414373372"></a><aname="p737414373372"></a>Gets and sets the hostname (without the port) in a URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p1560554514376"><aname="p1560554514376"></a><aname="p1560554514376"></a>Gets and sets a string that contains the whole URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p11181479377"><aname="p11181479377"></a><aname="p11181479377"></a>Gets a read-only string that contains the Unicode serialization of the origin of the represented URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p1299814373714"><aname="p1299814373714"></a><aname="p1299814373714"></a>Gets and sets the password in a URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p18527842143718"><aname="p18527842143718"></a><aname="p18527842143718"></a>Gets and sets the path in a URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p1421614403377"><aname="p1421614403377"></a><aname="p1421614403377"></a>Gets and sets the port in a URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p363383518374"><aname="p363383518374"></a><aname="p363383518374"></a>Gets and sets the protocol in a URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p12524144111358"><aname="p12524144111358"></a><aname="p12524144111358"></a>Gets and sets the serialized query string in a URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p8483124473515"><aname="p8483124473515"></a><aname="p8483124473515"></a>Gets a <strongid="b14942173842317"><aname="b14942173842317"></a><aname="b14942173842317"></a>URLSearchParams</strong> object allowing access to the query parameters in a URL.</p>
<tdclass="cellrowborder"valign="top"width="52.42%"headers="mcps1.1.6.1.5 "><pid="p348319442351"><aname="p348319442351"></a><aname="p348319442351"></a>Gets and sets the username in a URL.</p>
<td class="cellrowborder" valign="top" width="49.55%" headers="mcps1.1.5.1.4 "><p id="p16902126135313"><a name="p16902126135313"></a><a name="p16902126135313"></a>Input parameter, which can be any of the following:</p>
var a = new URL("/", mm); // Output 'http://username:password@host:8080/';
var b = new URL(mm); // Output 'http://username:password@host:8080/';
new URL('path/path1', b); // Output 'http://username:password@host:8080/path/path1';
var c = new URL('/path/path1', b); // Output 'http://username:password@host:8080/path/path1';
new URL('/path/path1', c); // Output 'http://username:password@host:8080/path/path1';
new URL('/path/path1', a); // Output 'http://username:password@host:8080/path/path1';
new URL('/path/path1', "https://www.exampleUrl/fr-FR/toto"); // Output https://www.exampleUrl/path/path1
new URL('/path/path1', ''); // Raises a TypeError exception as '' is not a valid URL
new URL('/path/path1'); // Raises a TypeError exception as '/path/path1' is not a valid URL
new URL('http://www.shanxi.com', ); // Output http://www.shanxi.com/
new URL('http://www.shanxi.com', b); // Output http://www.shanxi.com/
```
### tostring<a name="section2849448104520"></a>
toString\(\): string
Converts the parsed URL into a string.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| url | string | Yes| Input object.|
| base | string \| URL | No| Input parameter, which can be any of the following: <br/>- **string**: string<br/>- **URL**: string or object|
- Return values
- Example
```
var mm = 'http://username:password@host:8080';
var a = new URL("/", mm); // Output 'http://username:password@host:8080/';
var b = new URL(mm); // Output 'http://username:password@host:8080/';
new URL('path/path1', b); // Output 'http://username:password@host:8080/path/path1';
var c = new URL('/path/path1', b); // Output 'http://username:password@host:8080/path/path1';
new URL('/path/path1', c); // Output 'http://username:password@host:8080/path/path1';
new URL('/path/path1', a); // Output 'http://username:password@host:8080/path/path1';
new URL('/path/path1', "https://www.exampleUrl/fr-FR/toto"); // Output https://www.exampleUrl/path/path1
new URL('/path/path1', ''); // Raises a TypeError exception as '' is not a valid URL
new URL('/path/path1'); // Raises a TypeError exception as '/path/path1' is not a valid URL
new URL('http://www.shanxi.com', ); // Output http://www.shanxi.com/
new URL('http://www.shanxi.com', b); // Output http://www.shanxi.com/