>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|
- Example
```
var objectParams = new URLSearchParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]);
var objectParams1 = new URLSearchParams({"fod" : 1 , "bard" : 2});
...
...
@@ -58,48 +40,19 @@ Creates a **URLSearchParams** instance.
<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>
| 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));
...
...
@@ -107,40 +60,18 @@ Appends a key-value pair into the query string.
<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>
| 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));
...
...
@@ -148,58 +79,23 @@ Deletes key-value pairs of the specified key.
```
### getAll<a name="section2178125553415"></a>
### getAll
getAll\(name: string\): string\[\]
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.
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>
</tr>
</tbody>
</table>
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the key-value pairs in the **URLSearchParams** instance.|
| thisArg | Object | No| Value to use when the callback is invoked.|
<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>
<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>
| 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"
...
...
@@ -398,56 +179,22 @@ Obtains the value of the first key-value pair based on the specified key.
<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>
</tr>
</tbody>
</table>
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to search for its value.|
<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>
| 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));
...
...
@@ -455,49 +202,19 @@ Checks whether a key has a value.
```
### set<a name="section186091516203910"></a>
### set
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));
...
...
@@ -505,14 +222,15 @@ Sets the value for a key. If key-value pairs matching the specified key exist, t
```
### sort<a name="section102851552183915"></a>
### sort
sort\(\): void
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
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.
- 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
...
...
@@ -520,32 +238,21 @@ Sorts all key-value pairs contained in this object based on the Unicode code poi
```
### keys<a name="section816918183413"></a>
### keys
keys(): IterableIterator<string>
keys\(\): IterableIterator<string\>
Obtains an ES6 iterator that contains the keys of all the key-value pairs.
<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>
- 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
...
...
@@ -554,32 +261,18 @@ Obtains an ES6 iterator that contains the keys of all the key-value pairs.
```
### values<a name="section0542174314413"></a>
### values
values\(\): IterableIterator<string\>
values(): IterableIterator<string>
Obtains an ES6 iterator that contains the values of all the key-value pairs.
<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>
| 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
for (var value of searchParams.values()) {
...
...
@@ -588,32 +281,21 @@ Obtains an ES6 iterator that contains the values of all the key-value pairs.
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
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>
- Return values
| Type| Description|
| -------- | -------- |
| string | String of serialized search parameters, which is percent-encoded if necessary.|
- Example
- Example
```
let url = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new URLSearchParams(url.search.slice(1));
...
...
@@ -656,202 +327,43 @@ Returns search parameters that are serialized as a string and, if necessary, per
<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>
| base | string \| URL | No| Input parameter, which can be any of the following: <br/>- **string**: string<br/>- **URL**: string or object|
- Example
```
var mm = 'http://username:password@host:8080';
var a = new URL("/", mm); // Output 'http://username:password@host:8080/';