js-apis-url.md 25.6 KB
Newer Older
W
wusongqing 已提交
1
# 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 14


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

G
Gloria 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375
constructor(init?: string[][] | Record&lt;string, string&gt; | string | URLParams)

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

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| init | string[][] \| Record&lt;string, string&gt; \| string \| URLParams | 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>- **URLParams**: object|

**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');
let urlObject = new Url.URL('https://developer.mozilla.org/?fod=1&bard=2');
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
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
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
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsobject = new Url.URLParams(urlObject.search.slice(1));
paramsobject.delete('fod');
```


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

getAll(name: string): string[]

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

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain all key-value pairs.|

**Return value**

| Type| Description|
| -------- | -------- |
| string[] | All key-value pairs matching the specified key.|

**Example**

```js
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
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"); 
for (var pair of searchParamsObject .entries()) { // Show keyName/valueName pairs
    console.log(pair[0]+ ', '+ pair[1]);
}
```


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

forEach(callbackfn: (value: string, key: string, searchParams: this) => void, thisArg?: Object): void

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

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

**Parameters**

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

**Table 1** callbackfn parameter description

| 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
const myURLObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 
myURLObject.URLParams.forEach((value, name, searchParams) => {  
    console.log(name, value, myURLObject.searchParams === searchParams);
});
```


### 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
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
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
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
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
let url = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new Url.URLParams(url.search.slice(1)); 
params.append('fod', '3');
console.log(params.toString());
```

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

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

> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>](#constructor9+) instead.
Z
zengyawen 已提交
376

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

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

W
wusongqing 已提交
381 382
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
383 384 385 386
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
387
| 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 已提交
388 389

**Example**
Z
zengyawen 已提交
390

391
```js
392 393 394 395 396
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 已提交
397
```
Z
zengyawen 已提交
398

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

G
Gloria 已提交
401 402 403
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.append<sup>9+</sup>](#append9) instead.
Z
zengyawen 已提交
404

W
wusongqing 已提交
405
append(name: string, value: string): void
Z
zengyawen 已提交
406 407 408

Appends a key-value pair into the query string.

W
wusongqing 已提交
409 410
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
411 412
**Parameters**

W
wusongqing 已提交
413 414 415 416
| 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 已提交
417 418

**Example**
W
wusongqing 已提交
419

420
```js
421 422
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1));
S
shikai-123 已提交
423
paramsObject.append('fod', '3');
W
wusongqing 已提交
424
```
W
wusongqing 已提交
425

G
Gloria 已提交
426
### delete<sup>(deprecated)</sup>
W
wusongqing 已提交
427

G
Gloria 已提交
428 429 430
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.delete<sup>9+</sup>](#delete9) instead.
W
wusongqing 已提交
431 432

delete(name: string): void
Z
zengyawen 已提交
433 434 435

Deletes key-value pairs of the specified key.

W
wusongqing 已提交
436 437
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
438 439
**Parameters**

W
wusongqing 已提交
440 441 442
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the key-value pairs to delete.|
W
wusongqing 已提交
443 444

**Example**
W
wusongqing 已提交
445

446
```js
447 448
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsobject = new Url.URLSearchParams(urlObject.search.slice(1));
W
wusongqing 已提交
449
paramsobject.delete('fod');
W
wusongqing 已提交
450
```
W
wusongqing 已提交
451

G
Gloria 已提交
452
### getAll<sup>(deprecated)</sup>
W
wusongqing 已提交
453

G
Gloria 已提交
454 455 456
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.getAll<sup>9+</sup>](#getall9) instead.
W
wusongqing 已提交
457 458

getAll(name: string): string[]
Z
zengyawen 已提交
459 460 461

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

W
wusongqing 已提交
462 463
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
464 465
**Parameters**

W
wusongqing 已提交
466 467 468
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain all key-value pairs.|
W
wusongqing 已提交
469 470

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

W
wusongqing 已提交
472 473 474
| Type| Description|
| -------- | -------- |
| string[] | All key-value pairs matching the specified key.|
W
wusongqing 已提交
475

W
wusongqing 已提交
476 477
**Example**

478
```js
S
shikai-123 已提交
479 480 481 482
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 已提交
483
```
W
wusongqing 已提交
484

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

G
Gloria 已提交
487 488 489
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.entries<sup>9+</sup>](#entries9) instead.
W
wusongqing 已提交
490

X
xdmal 已提交
491
entries(): IterableIterator<[string, string]>
W
wusongqing 已提交
492 493 494

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.

W
wusongqing 已提交
495 496
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
497 498
**Return value**

W
wusongqing 已提交
499 500
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
501
| IterableIterator&lt;[string, string]&gt; | ES6 iterator.|
W
wusongqing 已提交
502

W
wusongqing 已提交
503 504
**Example**

505
```js
506
let searchParamsObject = new Url.URLSearchParams("keyName1=valueName1&keyName2=valueName2"); 
W
wusongqing 已提交
507 508 509 510
for (var pair of searchParamsObject .entries()) { // Show keyName/valueName pairs
    console.log(pair[0]+ ', '+ pair[1]);
}
```
W
wusongqing 已提交
511 512


G
Gloria 已提交
513 514 515 516
### forEach<sup>(deprecated)</sup>
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.forEach<sup>9+</sup>](#foreach9) instead.
W
wusongqing 已提交
517

X
xdmal 已提交
518
forEach(callbackfn: (value: string, key: string, searchParams: this) => void, thisArg?: Object): void
W
wusongqing 已提交
519 520 521

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

W
wusongqing 已提交
522 523
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
524 525
**Parameters**

W
wusongqing 已提交
526 527 528 529
| 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.|
W
wusongqing 已提交
530 531 532

**Table 1** callbackfn parameter description

W
wusongqing 已提交
533 534 535 536 537
| 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 已提交
538 539 540

**Example**

541
```js
542
const myURLObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 
W
wusongqing 已提交
543 544 545 546
myURLObject.searchParams.forEach((value, name, searchParams) => {  
    console.log(name, value, myURLObject.searchParams === searchParams);
});
```
W
wusongqing 已提交
547 548


G
Gloria 已提交
549 550 551 552
### get<sup>(deprecated)</sup>
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.get<sup>9+</sup>](#get9) instead.
W
wusongqing 已提交
553 554

get(name: string): string | null
Z
zengyawen 已提交
555 556 557

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

W
wusongqing 已提交
558 559
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
560 561
**Parameters**

W
wusongqing 已提交
562 563 564
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain the value.|
W
wusongqing 已提交
565 566

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

W
wusongqing 已提交
568 569 570
| Type| Description|
| -------- | -------- |
| string | Returns the value of the first key-value pair if obtained.|
G
Gloria 已提交
571
| null | Returns **null** if no value is obtained.|
W
wusongqing 已提交
572

W
wusongqing 已提交
573 574
**Example**

575
```js
Z
zengyawen 已提交
576 577 578
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 已提交
579
```
W
wusongqing 已提交
580 581


G
Gloria 已提交
582 583 584 585
### has<sup>(deprecated)</sup>
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.has<sup>9+</sup>](#has9) instead.
W
wusongqing 已提交
586 587

has(name: string): boolean
Z
zengyawen 已提交
588 589

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

W
wusongqing 已提交
591 592
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
593 594
**Parameters**

W
wusongqing 已提交
595 596 597
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to search for its value.|
W
wusongqing 已提交
598

W
wusongqing 已提交
599 600
**Return value**

W
wusongqing 已提交
601 602 603
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the value exists; returns **false** otherwise.|
W
wusongqing 已提交
604 605 606

**Example**

607
```js
608 609
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1)); 
W
wusongqing 已提交
610 611
paramsObject.has('bard') === true;
```
W
wusongqing 已提交
612 613


G
Gloria 已提交
614 615 616 617
### set<sup>(deprecated)</sup>
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.set<sup>9+</sup>](#set9) instead.
Z
zengyawen 已提交
618

W
wusongqing 已提交
619
set(name: string, value: string): void
Z
zengyawen 已提交
620 621 622

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.

W
wusongqing 已提交
623 624
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
625 626
**Parameters**

W
wusongqing 已提交
627 628 629 630
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the value to set.|
| value | string | Yes| Value to set.|
W
wusongqing 已提交
631

W
wusongqing 已提交
632 633
**Example**

634
```js
635 636
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1));
S
shikai-123 已提交
637
paramsObject.set('baz', '3'); // Add a third parameter.
W
wusongqing 已提交
638
```
W
wusongqing 已提交
639 640


G
Gloria 已提交
641 642 643 644
### sort<sup>(deprecated)</sup>
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.sort<sup>9+</sup>](#sort9) instead.
W
wusongqing 已提交
645 646

sort(): void
Z
zengyawen 已提交
647

W
wusongqing 已提交
648
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 已提交
649

W
wusongqing 已提交
650
**System capability**: SystemCapability.Utils.Lang
Z
zengyawen 已提交
651

W
wusongqing 已提交
652
**Example**
Z
zengyawen 已提交
653

654
```js
655
let searchParamsObject = new Url.URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object
W
wusongqing 已提交
656 657 658
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 已提交
659 660


G
Gloria 已提交
661 662 663 664
### keys<sup>(deprecated)</sup>
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.keys<sup>9+</sup>](#keys9) instead.
Z
zengyawen 已提交
665

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

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

W
wusongqing 已提交
670 671
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
672
**Return value**
W
wusongqing 已提交
673

W
wusongqing 已提交
674 675 676
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;string&gt; | ES6 iterator that contains the keys of all the key-value pairs.|
W
wusongqing 已提交
677

W
wusongqing 已提交
678
**Example**
W
wusongqing 已提交
679

680
```js
681
let searchParamsObject = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
W
wusongqing 已提交
682 683 684 685
for (var key of searchParamsObject .keys()) { // Output key-value pairs
    console.log(key);
}
```
W
wusongqing 已提交
686 687


G
Gloria 已提交
688 689 690 691
### values<sup>(deprecated)</sup>
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.values<sup>9+</sup>](#values9) instead.
W
wusongqing 已提交
692 693

values(): IterableIterator&lt;string&gt;
Z
zengyawen 已提交
694 695 696

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

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

W
wusongqing 已提交
699 700
**Return value**

W
wusongqing 已提交
701 702 703
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;string&gt; | ES6 iterator that contains the values of all the key-value pairs.|
W
wusongqing 已提交
704

W
wusongqing 已提交
705 706
**Example**

707
```js
708 709
let searchParams = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
for (var value of searchParams.values()) {
W
wusongqing 已提交
710 711 712
    console.log(value);
}
```
W
wusongqing 已提交
713 714


G
Gloria 已提交
715 716 717 718
### [Symbol.iterator]<sup>(deprecated)</sup>
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.[Symbol.iterator]<sup>9+</sup>](#symbol.iterator9) instead.
W
wusongqing 已提交
719

W
wusongqing 已提交
720
[Symbol.iterator]\(): IterableIterator&lt;[string, string]&gt;
W
wusongqing 已提交
721 722 723

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.

W
wusongqing 已提交
724 725
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
726
**Return value**
W
wusongqing 已提交
727

W
wusongqing 已提交
728 729
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
730
| IterableIterator&lt;[string, string]&gt; | ES6 iterator.|
W
wusongqing 已提交
731

W
wusongqing 已提交
732
**Example**
W
wusongqing 已提交
733

734
```js
735
const paramsObject = new Url.URLSearchParams('fod=bay&edg=bap');
736
for (const [name, value] of paramsObject) {
W
wusongqing 已提交
737 738 739
    console.log(name, value); 
} 
```
W
wusongqing 已提交
740

G
Gloria 已提交
741 742 743 744
### tostring<sup>(deprecated)</sup>
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use [URLParams<sup>9+</sup>.tostring<sup>9+</sup>](#tostring9) instead.
W
wusongqing 已提交
745 746 747 748 749

toString(): string

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

W
wusongqing 已提交
750 751
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
752
**Return value**
W
wusongqing 已提交
753

W
wusongqing 已提交
754 755 756
| Type| Description|
| -------- | -------- |
| string | String of serialized search parameters, which is percent-encoded if necessary.|
W
wusongqing 已提交
757

W
wusongqing 已提交
758
**Example**
W
wusongqing 已提交
759

760
```js
761 762
let url = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new Url.URLSearchParams(url.search.slice(1)); 
S
shikai-123 已提交
763
params.append('fod', '3');
W
wusongqing 已提交
764 765
console.log(params.toString());
```
W
wusongqing 已提交
766 767 768 769 770

## URL

### Attributes

W
wusongqing 已提交
771 772
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785
| 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.|
| searchParams | URLsearchParams | Yes| No| **URLSearchParams** object allowing access to the query parameters in a URL.|
G
Gloria 已提交
786
| URLParams | URLParams | Yes| No| **URLParams** object allowing access to the query parameters in a URL.|
W
wusongqing 已提交
787 788 789 790 791
| username | string | Yes| Yes| Username in a URL.|


### constructor

G
Gloria 已提交
792
constructor(url?: string, base?: string | URL)
Z
zengyawen 已提交
793 794 795

Creates a URL.

W
wusongqing 已提交
796 797
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
798
**Parameters**
Z
zengyawen 已提交
799

W
wusongqing 已提交
800 801 802
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| url | string | Yes| Input object.|
G
Gloria 已提交
803
| base | string \| URL | No| Input parameter, which can be any of the following:<br>- **string**: string<br>- **URL**: string or object|
W
wusongqing 已提交
804

W
wusongqing 已提交
805
**Example**
Z
zengyawen 已提交
806

807
```js
808 809 810
let mm = 'http://username:password@host:8080';
let a = new Url.URL("/", mm); // Output 'http://username:password@host:8080/';
let b = new Url.URL(mm); // Output 'http://username:password@host:8080/';
811
new Url.URL('path/path1', b); // Output 'http://username:password@host:8080/path/path1';
812
let c = new Url.URL('/path/path1', b);  // Output 'http://username:password@host:8080/path/path1'; 
813 814 815 816 817 818 819
new Url.URL('/path/path1', c); // Output 'http://username:password@host:8080/path/path1';
new Url.URL('/path/path1', a); // Output 'http://username:password@host:8080/path/path1';
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('http://www.shanxi.com', ); // Output http://www.shanxi.com/
new Url.URL('http://www.shanxi.com', b); // Output http://www.shanxi.com/
W
wusongqing 已提交
820
```
Z
zengyawen 已提交
821

G
Gloria 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
### parseURL<sup>9+</sup>

static parseURL(inputUrl : string, inputBase ?: string | URL)

Parses a URL.

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| inputUrl | string | Yes| Input object.|
| inputBase | string \| URL | No| Input parameter, which can be any of the following:<br>- **string**: string<br>- **URL**: string or object|

**Example**

```js
let mm = 'http://username:password@host:8080';
Url.URL.parseURL(mm); // Output 'http://username:password@host:8080/';
```
Z
zengyawen 已提交
843

W
wusongqing 已提交
844
### tostring
Z
zengyawen 已提交
845

W
wusongqing 已提交
846
toString(): string
Z
zengyawen 已提交
847

W
wusongqing 已提交
848
Converts the parsed URL into a string.
Z
zengyawen 已提交
849

W
wusongqing 已提交
850 851
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
852
**Return value**
Z
zengyawen 已提交
853

W
wusongqing 已提交
854 855 856
| Type| Description|
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
857

W
wusongqing 已提交
858
**Example**
Z
zengyawen 已提交
859

860
```js
861
const url = new Url.URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
G
Gloria 已提交
862
url.toString();
W
wusongqing 已提交
863
```
W
wusongqing 已提交
864

Z
zengyawen 已提交
865

W
wusongqing 已提交
866
### toJSON
Z
zengyawen 已提交
867

W
wusongqing 已提交
868
toJSON(): string
Z
zengyawen 已提交
869

W
wusongqing 已提交
870 871
Converts the parsed URL into a JSON string.

W
wusongqing 已提交
872 873
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
874
**Return value**
Z
zengyawen 已提交
875

W
wusongqing 已提交
876 877 878
| Type| Description|
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
879

W
wusongqing 已提交
880
**Example**
881
```js
882
const url = new Url.URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
G
Gloria 已提交
883
url.toJSON();
W
wusongqing 已提交
884
```
G
Gloria 已提交
885
<!--no_check-->