js-apis-url.md 26.7 KB
Newer Older
1
# @ohos.url (URL String Parsing)
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5
> 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.
Z
zengyawen 已提交
6

W
wusongqing 已提交
7
## Modules to Import
Z
zengyawen 已提交
8 9 10 11

```
import Url from '@ohos.url' 
```
G
Gloria 已提交
12
## URLParams<sup>9+</sup>
Z
zengyawen 已提交
13

G
Gloria 已提交
14
### constructor<sup>9+</sup>
Z
zengyawen 已提交
15

16
constructor(init?: string[][] | Record&lt;string, string&gt; | string | URLSearchParams)
G
Gloria 已提交
17 18 19 20 21 22 23 24 25

A constructor used to create a **URLParams** instance.

**System capability**: SystemCapability.Utils.Lang

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
26
| init | string[][] \| Record&lt;string, string&gt; \| string \| URLSearchParams | No| Input parameter objects, which include the following:<br>- **string[][]**: two-dimensional string array<br>- **Record&lt;string, string&gt;**: list of objects<br>- **string**: string<br>- **URLSearchParams**: object|
G
Gloria 已提交
27 28 29 30 31 32 33

**Example**

```js
let objectParams = new Url.URLParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]);
let objectParams1 = new Url.URLParams({"fod" : '1' , "bard" : '2'});
let objectParams2 = new Url.URLParams('?fod=1&bard=2');
34
let urlObject = Url.URL.parseURL('https://developer.mozilla.org/?fod=1&bard=2');
G
Gloria 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
let params = new Url.URLParams(urlObject.search);
```


### append<sup>9+</sup>

append(name: string, value: string): void

Appends a key-value pair into the query string.

**System capability**: SystemCapability.Utils.Lang

**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**

```js
57
let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
G
Gloria 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
let paramsObject = new Url.URLParams(urlObject.search.slice(1));
paramsObject.append('fod', '3');
```


### delete<sup>9+</sup>

delete(name: string): void

Deletes key-value pairs of the specified key.

**System capability**: SystemCapability.Utils.Lang

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the key-value pairs to delete.|

**Example**

```js
80 81 82
let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLParams(urlObject.search.slice(1));
paramsObject.delete('fod');
G
Gloria 已提交
83 84 85 86 87 88 89
```


### getAll<sup>9+</sup>

getAll(name: string): string[]

G
Gloria 已提交
90
Obtains all the values based on the specified key.
G
Gloria 已提交
91 92 93 94 95 96 97

**System capability**: SystemCapability.Utils.Lang

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
98
| name | string | Yes| Target key.|
G
Gloria 已提交
99 100 101 102 103

**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
104
| string[] | All the values obtained.|
G
Gloria 已提交
105 106 107 108

**Example**

```js
109
let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
G
Gloria 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
let params = new Url.URLParams(urlObject.search.slice(1));
params.append('fod', '3'); // Add a second value for the fod parameter.
console.log(params.getAll('fod').toString()) // Output ["1","3"].
```


### entries<sup>9+</sup>

entries(): IterableIterator<[string, string]>

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.

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;[string, string]&gt; | ES6 iterator.|

**Example**

```js
let searchParamsObject = new Url.URLParams("keyName1=valueName1&keyName2=valueName2"); 
134
for (var pair of searchParamsObject.entries()) { // Show keyName/valueName pairs
G
Gloria 已提交
135 136 137 138 139 140 141
    console.log(pair[0]+ ', '+ pair[1]);
}
```


### forEach<sup>9+</sup>

142
forEach(callbackFn: (value: string, key: string, searchParams: this) => void, thisArg?: Object): void
G
Gloria 已提交
143 144 145 146 147 148 149 150 151

Traverses the key-value pairs in the **URLSearchParams** instance by using a callback.

**System capability**: SystemCapability.Utils.Lang

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
152 153
| callbackFn | function | Yes| Callback invoked to traverse the key-value pairs in the **URLSearchParams** instance.|
| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked.|
G
Gloria 已提交
154

155
**Table 1** callbackFn parameter description
G
Gloria 已提交
156 157 158 159 160 161 162 163 164 165

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | string | Yes| Value that is currently traversed.|
| key | string | Yes| Key that is currently traversed.|
| searchParams | Object | Yes| Instance that invokes the **forEach** method.|

**Example**

```js
166 167 168
const myURLObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 
myURLObject.params.forEach((value, name, searchParams) => {  
    console.log(name, value, myURLObject.params === searchParams);
G
Gloria 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
});
```


### get<sup>9+</sup>

get(name: string): string | null

Obtains the value of the first key-value pair based on the specified key.

**System capability**: SystemCapability.Utils.Lang

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain the value.|

**Return value**

| Type| Description|
| -------- | -------- |
| string | Returns the value of the first key-value pair if obtained.|
| null | Returns **null** if no value is obtained.|

**Example**

```js
let paramsObject = new Url.URLParams('name=Jonathan&age=18'); 
let name = paramsObject.get("name"); // is the string "Jonathan" 
let age = parseInt(paramsObject.get("age"), 10); // is the number 18
```


### has<sup>9+</sup>

has(name: string): boolean

Checks whether a key has a value.

**System capability**: SystemCapability.Utils.Lang

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to search for its value.|

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the value exists; returns **false** otherwise.|

**Example**

```js
226
let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
G
Gloria 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
let paramsObject = new Url.URLParams(urlObject.search.slice(1)); 
paramsObject.has('bard') === true;
```


### set<sup>9+</sup>

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.

**System capability**: SystemCapability.Utils.Lang

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the value to set.|
| value | string | Yes| Value to set.|

**Example**

```js
250
let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
G
Gloria 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
let paramsObject = new Url.URLParams(urlObject.search.slice(1));
paramsObject.set('baz', '3'); // Add a third parameter.
```


### sort<sup>9+</sup>

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.

**System capability**: SystemCapability.Utils.Lang

**Example**

```js
let searchParamsObject = new Url.URLParams("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<sup>9+</sup>

keys(): IterableIterator&lt;string&gt;

Obtains an ES6 iterator that contains the keys of all the key-value pairs.

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;string&gt; | ES6 iterator that contains the keys of all the key-value pairs.|

**Example**

```js
let searchParamsObject = new Url.URLParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
for (var key of searchParamsObject .keys()) { // Output key-value pairs
    console.log(key);
}
```


### values<sup>9+</sup>

values(): IterableIterator&lt;string&gt;

Obtains an ES6 iterator that contains the values of all the key-value pairs.

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;string&gt; | ES6 iterator that contains the values of all the key-value pairs.|

**Example**

```js
let searchParams = new Url.URLParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
for (var value of searchParams.values()) {
    console.log(value);
}
```


### [Symbol.iterator]<sup>9+</sup>

[Symbol.iterator]\(): IterableIterator&lt;[string, string]&gt;

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.

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;[string, string]&gt; | ES6 iterator.|

**Example**

```js
const paramsObject = new Url.URLParams('fod=bay&edg=bap');
for (const [name, value] of paramsObject) {
    console.log(name, value); 
} 
```


### tostring<sup>9+</sup>

toString(): string

Obtains search parameters that are serialized as a string and, if necessary, percent-encodes the characters in the string.

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
| string | String of serialized search parameters, which is percent-encoded if necessary.|

**Example**

```js
362
let url = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
G
Gloria 已提交
363 364 365 366 367
let params = new Url.URLParams(url.search.slice(1)); 
params.append('fod', '3');
console.log(params.toString());
```

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
## URL

### Attributes

**System capability**: SystemCapability.Utils.Lang

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| hash | string | Yes| Yes| String that contains a harsh mark (#) followed by the fragment identifier of a URL.|
| host | string | Yes| Yes| Host information in a URL.|
| hostname | string | Yes| Yes| Hostname (without the port) in a URL.|
| href | string | Yes| Yes| String that contains the whole URL.|
| origin | string | Yes| No| Read-only string that contains the Unicode serialization of the origin of the represented URL.|
| password | string | Yes| Yes| Password in a URL.|
| pathname | string | Yes| Yes| Path in a URL.|
| port | string | Yes| Yes| Port in a URL.|
| protocol | string | Yes| Yes| Protocol in a URL.|
| search | string | Yes| Yes| Serialized query string in a URL.|
386 387
| searchParams<sup>(deprecated)</sup> | [URLSearchParams](#urlsearchparamsdeprecated) | Yes| No| **URLSearchParams** object allowing access to the query parameters in a URL.<br>- **NOTE**: This attribute is supported since API version 7 and is deprecated since API version 9. You are advised to use params<sup>9+</sup> instead.|
| params<sup>9+</sup> | [URLParams](#urlparams9) | Yes| No| **URLParams** object allowing access to the query parameters in a URL.|
388
| username | string | Yes| Yes| Username in a URL.|
G
Gloria 已提交
389 390 391 392 393

### constructor<sup>(deprecated)</sup>

> **NOTE**
>
394 395
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [parseURL<sup>9+</sup>](#parseurl9) instead.

396 397 398 399
constructor(url: string, base?: string | URL)

Creates a URL.

400 401 402 403 404 405 406 407 408 409 410 411
**System capability**: SystemCapability.Utils.Lang

**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|

**Example**

```js
412 413 414 415 416 417 418
let mm = 'https://username:password@host:8080';
let a = new Url.URL("/", mm); // Output 'https://username:password@host:8080/';
let b = new Url.URL(mm); // Output 'https://username:password@host:8080/';
new Url.URL('path/path1', b); // Output 'https://username:password@host:8080/path/path1';
let c = new Url.URL('/path/path1', b);  // Output 'https://username:password@host:8080/path/path1'; 
new Url.URL('/path/path1', c); // Output 'https://username:password@host:8080/path/path1';
new Url.URL('/path/path1', a); // Output 'https://username:password@host:8080/path/path1';
419 420 421 422 423 424 425
new Url.URL('/path/path1', "https://www.exampleUrl/fr-FR/toto"); // Output https://www.exampleUrl/path/path1
new Url.URL('/path/path1', ''); // Raises a TypeError exception as '' is not a valid URL
new Url.URL('/path/path1'); // Raises a TypeError exception as '/path/path1' is not a valid URL
new Url.URL('https://www.example.com', ); // Output https://www.example.com/
new Url.URL('https://www.example.com', b); // Output https://www.example.com/
```

426 427 428 429 430 431 432
### constructor<sup>9+</sup>

constructor()

A no-argument constructor used to create a URL. It returns a **URL** object after **parseURL** is called. It is not used independently.

**System capability**: SystemCapability.Utils.Lang
433

434
### parseURL<sup>9+</sup>
G
Gloria 已提交
435

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
static parseURL(url : string, base?: string | URL): URL

Parses a URL.

**System capability**: SystemCapability.Utils.Lang

**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|

**Error codes**

For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).

| ID| Error Message|
| -------- | -------- |
| 10200002 | Invalid url string. |

457

458 459 460
**Example**

```js
461 462 463
let mm = 'https://username:password@host:8080';
let url = Url.URL.parseURL(mm); 
url.toString(); // Output 'https://username:password@host:8080/';
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
```

### tostring

toString(): string

Converts the parsed URL into a string.

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
| string | Website address in a serialized string.|

**Example**

```js
483
const url = Url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
url.toString();
```

### toJSON

toJSON(): string

Converts the parsed URL into a JSON string.

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
| string | Website address in a serialized string.|

**Example**
```js
503
const url = Url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
504 505 506 507 508 509
url.toJSON();
```

## URLSearchParams<sup>(deprecated)</sup>

### constructor<sup>(deprecated)</sup>
Z
zengyawen 已提交
510

W
wusongqing 已提交
511
constructor(init?: string[][] | Record&lt;string, string&gt; | string | URLSearchParams)
Z
zengyawen 已提交
512

G
Gloria 已提交
513
A constructor used to create a **URLSearchParams** instance.
Z
zengyawen 已提交
514

515 516
> **NOTE**
>
G
Gloria 已提交
517
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.constructor<sup>9+</sup>](#constructor9) instead.
518

W
wusongqing 已提交
519 520
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
521 522 523 524
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
525
| init | string[][] \| Record&lt;string, string&gt; \| string \| URLSearchParams | No| Input parameter objects, which include the following:<br>- **string[][]**: two-dimensional string array<br>- **Record&lt;string, string&gt;**: list of objects<br>- **string**: string<br>- **URLSearchParams**: object|
W
wusongqing 已提交
526 527

**Example**
Z
zengyawen 已提交
528

529
```js
530 531 532 533 534
let objectParams = new Url.URLSearchParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]);
let objectParams1 = new Url.URLSearchParams({"fod" : '1' , "bard" : '2'});
let objectParams2 = new Url.URLSearchParams('?fod=1&bard=2');
let urlObject = new Url.URL('https://developer.mozilla.org/?fod=1&bard=2');
let params = new Url.URLSearchParams(urlObject.search);
W
wusongqing 已提交
535
```
Z
zengyawen 已提交
536

G
Gloria 已提交
537
### append<sup>(deprecated)</sup>
Z
zengyawen 已提交
538

W
wusongqing 已提交
539
append(name: string, value: string): void
Z
zengyawen 已提交
540 541 542

Appends a key-value pair into the query string.

543 544
> **NOTE**
>
G
Gloria 已提交
545
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.append<sup>9+</sup>](#append9) instead.
546

W
wusongqing 已提交
547 548
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
549 550
**Parameters**

W
wusongqing 已提交
551 552 553 554
| 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.|
W
wusongqing 已提交
555 556

**Example**
W
wusongqing 已提交
557

558
```js
559 560
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1));
S
shikai-123 已提交
561
paramsObject.append('fod', '3');
W
wusongqing 已提交
562
```
W
wusongqing 已提交
563

G
Gloria 已提交
564
### delete<sup>(deprecated)</sup>
W
wusongqing 已提交
565 566

delete(name: string): void
Z
zengyawen 已提交
567 568 569

Deletes key-value pairs of the specified key.

570 571
> **NOTE**
>
G
Gloria 已提交
572
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.delete<sup>9+</sup>](#delete9) instead.
573

W
wusongqing 已提交
574 575
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
576 577
**Parameters**

W
wusongqing 已提交
578 579 580
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the key-value pairs to delete.|
W
wusongqing 已提交
581 582

**Example**
W
wusongqing 已提交
583

584
```js
585 586
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsobject = new Url.URLSearchParams(urlObject.search.slice(1));
W
wusongqing 已提交
587
paramsobject.delete('fod');
W
wusongqing 已提交
588
```
W
wusongqing 已提交
589

G
Gloria 已提交
590
### getAll<sup>(deprecated)</sup>
W
wusongqing 已提交
591 592

getAll(name: string): string[]
Z
zengyawen 已提交
593 594 595

Obtains all the key-value pairs based on the specified key.

596 597
> **NOTE**
>
G
Gloria 已提交
598
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.getAll<sup>9+</sup>](#getall9) instead.
599

W
wusongqing 已提交
600 601
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
602 603
**Parameters**

W
wusongqing 已提交
604 605
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
606
| name | string | Yes| Target key.|
W
wusongqing 已提交
607 608

**Return value**
W
wusongqing 已提交
609

W
wusongqing 已提交
610 611 612
| Type| Description|
| -------- | -------- |
| string[] | All key-value pairs matching the specified key.|
W
wusongqing 已提交
613

W
wusongqing 已提交
614 615
**Example**

616
```js
S
shikai-123 已提交
617 618 619 620
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new Url.URLSearchParams(urlObject.search.slice(1));
params.append('fod', '3'); // Add a second value for the fod parameter.
console.log(params.getAll('fod').toString()) // Output ["1","3"].
W
wusongqing 已提交
621
```
W
wusongqing 已提交
622

G
Gloria 已提交
623
### entries<sup>(deprecated)</sup>
W
wusongqing 已提交
624

X
xdmal 已提交
625
entries(): IterableIterator<[string, string]>
W
wusongqing 已提交
626 627 628

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.

629 630
> **NOTE**
>
G
Gloria 已提交
631
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.entries<sup>9+</sup>](#entries9) instead.
632

W
wusongqing 已提交
633 634
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
635 636
**Return value**

W
wusongqing 已提交
637 638
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
639
| IterableIterator&lt;[string, string]&gt; | ES6 iterator.|
W
wusongqing 已提交
640

W
wusongqing 已提交
641 642
**Example**

643
```js
644
let searchParamsObject = new Url.URLSearchParams("keyName1=valueName1&keyName2=valueName2"); 
645
for (var pair of searchParamsObject.entries()) { // Show keyName/valueName pairs
W
wusongqing 已提交
646 647 648
    console.log(pair[0]+ ', '+ pair[1]);
}
```
W
wusongqing 已提交
649 650


G
Gloria 已提交
651
### forEach<sup>(deprecated)</sup>
W
wusongqing 已提交
652

653
forEach(callbackFn: (value: string, key: string, searchParams: this) => void, thisArg?: Object): void
W
wusongqing 已提交
654 655 656

Traverses the key-value pairs in the **URLSearchParams** instance by using a callback.

657 658
> **NOTE**
>
G
Gloria 已提交
659
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.forEach<sup>9+</sup>](#foreach9) instead.
660

W
wusongqing 已提交
661 662
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
663 664
**Parameters**

W
wusongqing 已提交
665 666
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
667
| callbackFn | function | Yes| Callback invoked to traverse the key-value pairs in the **URLSearchParams** instance.|
668
| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked.|
W
wusongqing 已提交
669

670
**Table 1** callbackFn parameter description
W
wusongqing 已提交
671

W
wusongqing 已提交
672 673 674 675 676
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | string | Yes| Value that is currently traversed.|
| key | string | Yes| Key that is currently traversed.|
| searchParams | Object | Yes| Instance that invokes the **forEach** method.|
W
wusongqing 已提交
677 678 679

**Example**

680
```js
681
const myURLObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 
W
wusongqing 已提交
682 683 684 685
myURLObject.searchParams.forEach((value, name, searchParams) => {  
    console.log(name, value, myURLObject.searchParams === searchParams);
});
```
W
wusongqing 已提交
686 687


G
Gloria 已提交
688
### get<sup>(deprecated)</sup>
W
wusongqing 已提交
689 690

get(name: string): string | null
Z
zengyawen 已提交
691 692 693

Obtains the value of the first key-value pair based on the specified key.

694 695
> **NOTE**
>
G
Gloria 已提交
696
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.get<sup>9+</sup>](#get9) instead.
697

W
wusongqing 已提交
698 699
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
700 701
**Parameters**

W
wusongqing 已提交
702 703 704
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain the value.|
W
wusongqing 已提交
705 706

**Return value**
W
wusongqing 已提交
707

W
wusongqing 已提交
708 709 710
| Type| Description|
| -------- | -------- |
| string | Returns the value of the first key-value pair if obtained.|
G
Gloria 已提交
711
| null | Returns **null** if no value is obtained.|
W
wusongqing 已提交
712

W
wusongqing 已提交
713 714
**Example**

715
```js
Z
zengyawen 已提交
716 717 718
let paramsObject = new Url.URLSearchParams('name=Jonathan&age=18'); 
let name = paramsObject.get("name"); // is the string "Jonathan" 
let age = parseInt(paramsObject.get("age"), 10); // is the number 18
W
wusongqing 已提交
719
```
W
wusongqing 已提交
720 721


G
Gloria 已提交
722
### has<sup>(deprecated)</sup>
W
wusongqing 已提交
723 724

has(name: string): boolean
Z
zengyawen 已提交
725 726

Checks whether a key has a value.
W
wusongqing 已提交
727

728 729
> **NOTE**
>
G
Gloria 已提交
730
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.has<sup>9+</sup>](#has9) instead.
731

W
wusongqing 已提交
732 733
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
734 735
**Parameters**

W
wusongqing 已提交
736 737 738
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to search for its value.|
W
wusongqing 已提交
739

W
wusongqing 已提交
740 741
**Return value**

W
wusongqing 已提交
742 743 744
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the value exists; returns **false** otherwise.|
W
wusongqing 已提交
745 746 747

**Example**

748
```js
749 750
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1)); 
W
wusongqing 已提交
751 752
paramsObject.has('bard') === true;
```
W
wusongqing 已提交
753 754


G
Gloria 已提交
755
### set<sup>(deprecated)</sup>
Z
zengyawen 已提交
756

W
wusongqing 已提交
757
set(name: string, value: string): void
Z
zengyawen 已提交
758 759 760

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.

761 762
> **NOTE**
>
G
Gloria 已提交
763
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.set<sup>9+</sup>](#set9) instead.
764

W
wusongqing 已提交
765 766
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
767 768
**Parameters**

W
wusongqing 已提交
769 770 771 772
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the value to set.|
| value | string | Yes| Value to set.|
W
wusongqing 已提交
773

W
wusongqing 已提交
774 775
**Example**

776
```js
777 778
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1));
S
shikai-123 已提交
779
paramsObject.set('baz', '3'); // Add a third parameter.
W
wusongqing 已提交
780
```
W
wusongqing 已提交
781 782


G
Gloria 已提交
783
### sort<sup>(deprecated)</sup>
W
wusongqing 已提交
784 785

sort(): void
Z
zengyawen 已提交
786

W
wusongqing 已提交
787
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.
Z
zengyawen 已提交
788

789 790
> **NOTE**
>
G
Gloria 已提交
791
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.sort<sup>9+</sup>](#sort9) instead.
792

W
wusongqing 已提交
793
**System capability**: SystemCapability.Utils.Lang
Z
zengyawen 已提交
794

W
wusongqing 已提交
795
**Example**
Z
zengyawen 已提交
796

797
```js
798
let searchParamsObject = new Url.URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object
W
wusongqing 已提交
799 800 801
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
```
Z
zengyawen 已提交
802 803


G
Gloria 已提交
804
### keys<sup>(deprecated)</sup>
Z
zengyawen 已提交
805

W
wusongqing 已提交
806
keys(): IterableIterator&lt;string&gt;
Z
zengyawen 已提交
807

W
wusongqing 已提交
808 809
Obtains an ES6 iterator that contains the keys of all the key-value pairs.

810 811
> **NOTE**
>
G
Gloria 已提交
812
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.keys<sup>9+</sup>](#keys9) instead.
813

W
wusongqing 已提交
814 815
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
816
**Return value**
W
wusongqing 已提交
817

W
wusongqing 已提交
818 819 820
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;string&gt; | ES6 iterator that contains the keys of all the key-value pairs.|
W
wusongqing 已提交
821

W
wusongqing 已提交
822
**Example**
W
wusongqing 已提交
823

824
```js
825
let searchParamsObject = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
W
wusongqing 已提交
826 827 828 829
for (var key of searchParamsObject .keys()) { // Output key-value pairs
    console.log(key);
}
```
W
wusongqing 已提交
830 831


G
Gloria 已提交
832
### values<sup>(deprecated)</sup>
W
wusongqing 已提交
833 834

values(): IterableIterator&lt;string&gt;
Z
zengyawen 已提交
835 836 837

Obtains an ES6 iterator that contains the values of all the key-value pairs.

838 839
> **NOTE**
>
G
Gloria 已提交
840
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.values<sup>9+</sup>](#values9) instead.
841

W
wusongqing 已提交
842 843
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
844 845
**Return value**

W
wusongqing 已提交
846 847 848
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;string&gt; | ES6 iterator that contains the values of all the key-value pairs.|
W
wusongqing 已提交
849

W
wusongqing 已提交
850 851
**Example**

852
```js
853 854
let searchParams = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
for (var value of searchParams.values()) {
W
wusongqing 已提交
855 856 857
    console.log(value);
}
```
W
wusongqing 已提交
858 859


G
Gloria 已提交
860
### [Symbol.iterator]<sup>(deprecated)</sup>
G
ge-yafang 已提交
861

W
wusongqing 已提交
862
[Symbol.iterator]\(): IterableIterator&lt;[string, string]&gt;
W
wusongqing 已提交
863 864 865

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.

866 867
> **NOTE**
>
G
Gloria 已提交
868
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.[Symbol.iterator]<sup>9+</sup>](#symboliterator9) instead.
869

W
wusongqing 已提交
870 871
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
872
**Return value**
W
wusongqing 已提交
873

W
wusongqing 已提交
874 875
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
876
| IterableIterator&lt;[string, string]&gt; | ES6 iterator.|
W
wusongqing 已提交
877

W
wusongqing 已提交
878
**Example**
W
wusongqing 已提交
879

880
```js
881
const paramsObject = new Url.URLSearchParams('fod=bay&edg=bap');
882
for (const [name, value] of paramsObject) {
W
wusongqing 已提交
883 884 885
    console.log(name, value); 
} 
```
W
wusongqing 已提交
886

G
Gloria 已提交
887
### tostring<sup>(deprecated)</sup>
W
wusongqing 已提交
888 889 890 891 892

toString(): string

Obtains search parameters that are serialized as a string and, if necessary, percent-encodes the characters in the string.

893 894
> **NOTE**
>
G
Gloria 已提交
895
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.tostring<sup>9+</sup>](#tostring9) instead.
896

W
wusongqing 已提交
897 898
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
899
**Return value**
W
wusongqing 已提交
900

W
wusongqing 已提交
901 902 903
| Type| Description|
| -------- | -------- |
| string | String of serialized search parameters, which is percent-encoded if necessary.|
W
wusongqing 已提交
904

W
wusongqing 已提交
905
**Example**
W
wusongqing 已提交
906

907
```js
908 909
let url = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new Url.URLSearchParams(url.search.slice(1)); 
S
shikai-123 已提交
910
params.append('fod', '3');
W
wusongqing 已提交
911 912
console.log(params.toString());
```
G
Gloria 已提交
913
<!--no_check-->