1# @ohos.url (URL String Parsing) 2 3> **NOTE** 4> 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. 6 7## Modules to Import 8 9``` 10import Url from '@ohos.url' 11``` 12## URLParams<sup>9+</sup> 13 14### constructor<sup>9+</sup> 15 16constructor(init?: string[][] | Record<string, string> | string | URLSearchParams) 17 18A constructor used to create a **URLParams** instance. 19 20**System capability**: SystemCapability.Utils.Lang 21 22**Parameters** 23 24| Name| Type| Mandatory| Description| 25| -------- | -------- | -------- | -------- | 26| 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| 27 28**Example** 29 30```js 31let objectParams = new Url.URLParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]); 32let objectParams1 = new Url.URLParams({"fod" : '1' , "bard" : '2'}); 33let objectParams2 = new Url.URLParams('?fod=1&bard=2'); 34let urlObject = Url.URL.parseURL('https://developer.mozilla.org/?fod=1&bard=2'); 35let params = new Url.URLParams(urlObject.search); 36``` 37 38 39### append<sup>9+</sup> 40 41append(name: string, value: string): void 42 43Appends a key-value pair into the query string. 44 45**System capability**: SystemCapability.Utils.Lang 46 47**Parameters** 48 49| Name| Type| Mandatory| Description| 50| -------- | -------- | -------- | -------- | 51| name | string | Yes| Key of the key-value pair to append.| 52| value | string | Yes| Value of the key-value pair to append.| 53 54**Example** 55 56```js 57let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 58let paramsObject = new Url.URLParams(urlObject.search.slice(1)); 59paramsObject.append('fod', '3'); 60``` 61 62 63### delete<sup>9+</sup> 64 65delete(name: string): void 66 67Deletes key-value pairs of the specified key. 68 69**System capability**: SystemCapability.Utils.Lang 70 71**Parameters** 72 73| Name| Type| Mandatory| Description| 74| -------- | -------- | -------- | -------- | 75| name | string | Yes| Key of the key-value pairs to delete.| 76 77**Example** 78 79```js 80let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 81let paramsObject = new Url.URLParams(urlObject.search.slice(1)); 82paramsObject.delete('fod'); 83``` 84 85 86### getAll<sup>9+</sup> 87 88getAll(name: string): string[] 89 90Obtains all the values based on the specified key. 91 92**System capability**: SystemCapability.Utils.Lang 93 94**Parameters** 95 96| Name| Type| Mandatory| Description| 97| -------- | -------- | -------- | -------- | 98| name | string | Yes| Target key.| 99 100**Return value** 101 102| Type| Description| 103| -------- | -------- | 104| string[] | All the values obtained.| 105 106**Example** 107 108```js 109let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 110let params = new Url.URLParams(urlObject.search.slice(1)); 111params.append('fod', '3'); // Add a second value for the fod parameter. 112console.log(params.getAll('fod').toString()) // Output ["1","3"]. 113``` 114 115 116### entries<sup>9+</sup> 117 118entries(): IterableIterator<[string, string]> 119 120Obtains 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. 121 122**System capability**: SystemCapability.Utils.Lang 123 124**Return value** 125 126| Type| Description| 127| -------- | -------- | 128| IterableIterator<[string, string]> | ES6 iterator.| 129 130**Example** 131 132```js 133let searchParamsObject = new Url.URLParams("keyName1=valueName1&keyName2=valueName2"); 134for (var pair of searchParamsObject.entries()) { // Show keyName/valueName pairs 135 console.log(pair[0]+ ', '+ pair[1]); 136} 137``` 138 139 140### forEach<sup>9+</sup> 141 142forEach(callbackFn: (value: string, key: string, searchParams: this) => void, thisArg?: Object): void 143 144Traverses the key-value pairs in the **URLSearchParams** instance by using a callback. 145 146**System capability**: SystemCapability.Utils.Lang 147 148**Parameters** 149 150| Name| Type| Mandatory| Description| 151| -------- | -------- | -------- | -------- | 152| callbackFn | function | Yes| Callback invoked to traverse the key-value pairs in the **URLSearchParams** instance.| 153| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked.| 154 155**Table 1** callbackFn parameter description 156 157| Name| Type| Mandatory| Description| 158| -------- | -------- | -------- | -------- | 159| value | string | Yes| Value that is currently traversed.| 160| key | string | Yes| Key that is currently traversed.| 161| searchParams | Object | Yes| Instance that invokes the **forEach** method.| 162 163**Example** 164 165```js 166const myURLObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 167myURLObject.params.forEach((value, name, searchParams) => { 168 console.log(name, value, myURLObject.params === searchParams); 169}); 170``` 171 172 173### get<sup>9+</sup> 174 175get(name: string): string | null 176 177Obtains the value of the first key-value pair based on the specified key. 178 179**System capability**: SystemCapability.Utils.Lang 180 181**Parameters** 182 183| Name| Type| Mandatory| Description| 184| -------- | -------- | -------- | -------- | 185| name | string | Yes| Key specified to obtain the value.| 186 187**Return value** 188 189| Type| Description| 190| -------- | -------- | 191| string | Returns the value of the first key-value pair if obtained.| 192| null | Returns **null** if no value is obtained.| 193 194**Example** 195 196```js 197let paramsObject = new Url.URLParams('name=Jonathan&age=18'); 198let name = paramsObject.get("name"); // is the string "Jonathan" 199let age = parseInt(paramsObject.get("age"), 10); // is the number 18 200``` 201 202 203### has<sup>9+</sup> 204 205has(name: string): boolean 206 207Checks whether a key has a value. 208 209**System capability**: SystemCapability.Utils.Lang 210 211**Parameters** 212 213| Name| Type| Mandatory| Description| 214| -------- | -------- | -------- | -------- | 215| name | string | Yes| Key specified to search for its value.| 216 217**Return value** 218 219| Type| Description| 220| -------- | -------- | 221| boolean | Returns **true** if the value exists; returns **false** otherwise.| 222 223**Example** 224 225```js 226let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 227let paramsObject = new Url.URLParams(urlObject.search.slice(1)); 228paramsObject.has('bard') === true; 229``` 230 231 232### set<sup>9+</sup> 233 234set(name: string, value: string): void 235 236Sets 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. 237 238**System capability**: SystemCapability.Utils.Lang 239 240**Parameters** 241 242| Name| Type| Mandatory| Description| 243| -------- | -------- | -------- | -------- | 244| name | string | Yes| Key of the value to set.| 245| value | string | Yes| Value to set.| 246 247**Example** 248 249```js 250let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 251let paramsObject = new Url.URLParams(urlObject.search.slice(1)); 252paramsObject.set('baz', '3'); // Add a third parameter. 253``` 254 255 256### sort<sup>9+</sup> 257 258sort(): void 259 260Sorts 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. 261 262**System capability**: SystemCapability.Utils.Lang 263 264**Example** 265 266```js 267let searchParamsObject = new Url.URLParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object 268searchParamsObject.sort(); // Sort the key/value pairs 269console.log(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=2&c=3&d=4 270``` 271 272 273### keys<sup>9+</sup> 274 275keys(): IterableIterator<string> 276 277Obtains an ES6 iterator that contains the keys of all the key-value pairs. 278 279**System capability**: SystemCapability.Utils.Lang 280 281**Return value** 282 283| Type| Description| 284| -------- | -------- | 285| IterableIterator<string> | ES6 iterator that contains the keys of all the key-value pairs.| 286 287**Example** 288 289```js 290let searchParamsObject = new Url.URLParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 291for (var key of searchParamsObject .keys()) { // Output key-value pairs 292 console.log(key); 293} 294``` 295 296 297### values<sup>9+</sup> 298 299values(): IterableIterator<string> 300 301Obtains an ES6 iterator that contains the values of all the key-value pairs. 302 303**System capability**: SystemCapability.Utils.Lang 304 305**Return value** 306 307| Type| Description| 308| -------- | -------- | 309| IterableIterator<string> | ES6 iterator that contains the values of all the key-value pairs.| 310 311**Example** 312 313```js 314let searchParams = new Url.URLParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 315for (var value of searchParams.values()) { 316 console.log(value); 317} 318``` 319 320 321### [Symbol.iterator]<sup>9+</sup> 322 323[Symbol.iterator]\(): IterableIterator<[string, string]> 324 325Obtains 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. 326 327**System capability**: SystemCapability.Utils.Lang 328 329**Return value** 330 331| Type| Description| 332| -------- | -------- | 333| IterableIterator<[string, string]> | ES6 iterator.| 334 335**Example** 336 337```js 338const paramsObject = new Url.URLParams('fod=bay&edg=bap'); 339for (const [name, value] of paramsObject) { 340 console.log(name, value); 341} 342``` 343 344 345### tostring<sup>9+</sup> 346 347toString(): string 348 349Obtains search parameters that are serialized as a string and, if necessary, percent-encodes the characters in the string. 350 351**System capability**: SystemCapability.Utils.Lang 352 353**Return value** 354 355| Type| Description| 356| -------- | -------- | 357| string | String of serialized search parameters, which is percent-encoded if necessary.| 358 359**Example** 360 361```js 362let url = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 363let params = new Url.URLParams(url.search.slice(1)); 364params.append('fod', '3'); 365console.log(params.toString()); 366``` 367 368## URL 369 370### Attributes 371 372**System capability**: SystemCapability.Utils.Lang 373 374| Name| Type| Readable| Writable| Description| 375| -------- | -------- | -------- | -------- | -------- | 376| hash | string | Yes| Yes| String that contains a harsh mark (#) followed by the fragment identifier of a URL.| 377| host | string | Yes| Yes| Host information in a URL.| 378| hostname | string | Yes| Yes| Hostname (without the port) in a URL.| 379| href | string | Yes| Yes| String that contains the whole URL.| 380| origin | string | Yes| No| Read-only string that contains the Unicode serialization of the origin of the represented URL.| 381| password | string | Yes| Yes| Password in a URL.| 382| pathname | string | Yes| Yes| Path in a URL.| 383| port | string | Yes| Yes| Port in a URL.| 384| protocol | string | Yes| Yes| Protocol in a URL.| 385| search | string | Yes| Yes| Serialized query string in a URL.| 386| 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.| 387| 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.| 389 390### constructor<sup>(deprecated)</sup> 391 392> **NOTE** 393> 394> 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. 395 396constructor(url: string, base?: string | URL) 397 398Creates a URL. 399 400**System capability**: SystemCapability.Utils.Lang 401 402**Parameters** 403 404| Name| Type| Mandatory| Description| 405| -------- | -------- | -------- | -------- | 406| url | string | Yes| Input object.| 407| base | string \| URL | No| Input parameter, which can be any of the following:<br>- **string**: string<br>- **URL**: string or object| 408 409**Example** 410 411```js 412let mm = 'https://username:password@host:8080'; 413let a = new Url.URL("/", mm); // Output 'https://username:password@host:8080/'; 414let b = new Url.URL(mm); // Output 'https://username:password@host:8080/'; 415new Url.URL('path/path1', b); // Output 'https://username:password@host:8080/path/path1'; 416let c = new Url.URL('/path/path1', b); // Output 'https://username:password@host:8080/path/path1'; 417new Url.URL('/path/path1', c); // Output 'https://username:password@host:8080/path/path1'; 418new Url.URL('/path/path1', a); // Output 'https://username:password@host:8080/path/path1'; 419new Url.URL('/path/path1', "https://www.exampleUrl/fr-FR/toto"); // Output https://www.exampleUrl/path/path1 420new Url.URL('/path/path1', ''); // Raises a TypeError exception as '' is not a valid URL 421new Url.URL('/path/path1'); // Raises a TypeError exception as '/path/path1' is not a valid URL 422new Url.URL('https://www.example.com', ); // Output https://www.example.com/ 423new Url.URL('https://www.example.com', b); // Output https://www.example.com/ 424``` 425 426### constructor<sup>9+</sup> 427 428constructor() 429 430A no-argument constructor used to create a URL. It returns a **URL** object after **parseURL** is called. It is not used independently. 431 432**System capability**: SystemCapability.Utils.Lang 433 434### parseURL<sup>9+</sup> 435 436static parseURL(url : string, base?: string | URL): URL 437 438Parses a URL. 439 440**System capability**: SystemCapability.Utils.Lang 441 442**Parameters** 443 444| Name| Type| Mandatory| Description| 445| -------- | -------- | -------- | -------- | 446| url | string | Yes| Input object.| 447| base | string \| URL | No| Input parameter, which can be any of the following:<br>- **string**: string<br>- **URL**: string or object| 448 449**Error codes** 450 451For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 452 453| ID| Error Message| 454| -------- | -------- | 455| 10200002 | Invalid url string. | 456 457 458**Example** 459 460```js 461let mm = 'https://username:password@host:8080'; 462let url = Url.URL.parseURL(mm); 463url.toString(); // Output 'https://username:password@host:8080/'; 464``` 465 466### tostring 467 468toString(): string 469 470Converts the parsed URL into a string. 471 472**System capability**: SystemCapability.Utils.Lang 473 474**Return value** 475 476| Type| Description| 477| -------- | -------- | 478| string | Website address in a serialized string.| 479 480**Example** 481 482```js 483const url = Url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 484url.toString(); 485``` 486 487### toJSON 488 489toJSON(): string 490 491Converts the parsed URL into a JSON string. 492 493**System capability**: SystemCapability.Utils.Lang 494 495**Return value** 496 497| Type| Description| 498| -------- | -------- | 499| string | Website address in a serialized string.| 500 501**Example** 502```js 503const url = Url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 504url.toJSON(); 505``` 506 507## URLSearchParams<sup>(deprecated)</sup> 508 509### constructor<sup>(deprecated)</sup> 510 511constructor(init?: string[][] | Record<string, string> | string | URLSearchParams) 512 513A constructor used to create a **URLSearchParams** instance. 514 515> **NOTE** 516> 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 519**System capability**: SystemCapability.Utils.Lang 520 521**Parameters** 522 523| Name| Type| Mandatory| Description| 524| -------- | -------- | -------- | -------- | 525| 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| 526 527**Example** 528 529```js 530let objectParams = new Url.URLSearchParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]); 531let objectParams1 = new Url.URLSearchParams({"fod" : '1' , "bard" : '2'}); 532let objectParams2 = new Url.URLSearchParams('?fod=1&bard=2'); 533let urlObject = new Url.URL('https://developer.mozilla.org/?fod=1&bard=2'); 534let params = new Url.URLSearchParams(urlObject.search); 535``` 536 537### append<sup>(deprecated)</sup> 538 539append(name: string, value: string): void 540 541Appends a key-value pair into the query string. 542 543> **NOTE** 544> 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 547**System capability**: SystemCapability.Utils.Lang 548 549**Parameters** 550 551| Name| Type| Mandatory| Description| 552| -------- | -------- | -------- | -------- | 553| name | string | Yes| Key of the key-value pair to append.| 554| value | string | Yes| Value of the key-value pair to append.| 555 556**Example** 557 558```js 559let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 560let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1)); 561paramsObject.append('fod', '3'); 562``` 563 564### delete<sup>(deprecated)</sup> 565 566delete(name: string): void 567 568Deletes key-value pairs of the specified key. 569 570> **NOTE** 571> 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 574**System capability**: SystemCapability.Utils.Lang 575 576**Parameters** 577 578| Name| Type| Mandatory| Description| 579| -------- | -------- | -------- | -------- | 580| name | string | Yes| Key of the key-value pairs to delete.| 581 582**Example** 583 584```js 585let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 586let paramsobject = new Url.URLSearchParams(urlObject.search.slice(1)); 587paramsobject.delete('fod'); 588``` 589 590### getAll<sup>(deprecated)</sup> 591 592getAll(name: string): string[] 593 594Obtains all the key-value pairs based on the specified key. 595 596> **NOTE** 597> 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 600**System capability**: SystemCapability.Utils.Lang 601 602**Parameters** 603 604| Name| Type| Mandatory| Description| 605| -------- | -------- | -------- | -------- | 606| name | string | Yes| Target key.| 607 608**Return value** 609 610| Type| Description| 611| -------- | -------- | 612| string[] | All key-value pairs matching the specified key.| 613 614**Example** 615 616```js 617let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 618let params = new Url.URLSearchParams(urlObject.search.slice(1)); 619params.append('fod', '3'); // Add a second value for the fod parameter. 620console.log(params.getAll('fod').toString()) // Output ["1","3"]. 621``` 622 623### entries<sup>(deprecated)</sup> 624 625entries(): IterableIterator<[string, string]> 626 627Obtains 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. 628 629> **NOTE** 630> 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 633**System capability**: SystemCapability.Utils.Lang 634 635**Return value** 636 637| Type| Description| 638| -------- | -------- | 639| IterableIterator<[string, string]> | ES6 iterator.| 640 641**Example** 642 643```js 644let searchParamsObject = new Url.URLSearchParams("keyName1=valueName1&keyName2=valueName2"); 645for (var pair of searchParamsObject.entries()) { // Show keyName/valueName pairs 646 console.log(pair[0]+ ', '+ pair[1]); 647} 648``` 649 650 651### forEach<sup>(deprecated)</sup> 652 653forEach(callbackFn: (value: string, key: string, searchParams: this) => void, thisArg?: Object): void 654 655Traverses the key-value pairs in the **URLSearchParams** instance by using a callback. 656 657> **NOTE** 658> 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 661**System capability**: SystemCapability.Utils.Lang 662 663**Parameters** 664 665| Name| Type| Mandatory| Description| 666| -------- | -------- | -------- | -------- | 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.| 669 670**Table 1** callbackFn parameter description 671 672| Name| Type| Mandatory| Description| 673| -------- | -------- | -------- | -------- | 674| value | string | Yes| Value that is currently traversed.| 675| key | string | Yes| Key that is currently traversed.| 676| searchParams | Object | Yes| Instance that invokes the **forEach** method.| 677 678**Example** 679 680```js 681const myURLObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 682myURLObject.searchParams.forEach((value, name, searchParams) => { 683 console.log(name, value, myURLObject.searchParams === searchParams); 684}); 685``` 686 687 688### get<sup>(deprecated)</sup> 689 690get(name: string): string | null 691 692Obtains the value of the first key-value pair based on the specified key. 693 694> **NOTE** 695> 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 698**System capability**: SystemCapability.Utils.Lang 699 700**Parameters** 701 702| Name| Type| Mandatory| Description| 703| -------- | -------- | -------- | -------- | 704| name | string | Yes| Key specified to obtain the value.| 705 706**Return value** 707 708| Type| Description| 709| -------- | -------- | 710| string | Returns the value of the first key-value pair if obtained.| 711| null | Returns **null** if no value is obtained.| 712 713**Example** 714 715```js 716let paramsObject = new Url.URLSearchParams('name=Jonathan&age=18'); 717let name = paramsObject.get("name"); // is the string "Jonathan" 718let age = parseInt(paramsObject.get("age"), 10); // is the number 18 719``` 720 721 722### has<sup>(deprecated)</sup> 723 724has(name: string): boolean 725 726Checks whether a key has a value. 727 728> **NOTE** 729> 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 732**System capability**: SystemCapability.Utils.Lang 733 734**Parameters** 735 736| Name| Type| Mandatory| Description| 737| -------- | -------- | -------- | -------- | 738| name | string | Yes| Key specified to search for its value.| 739 740**Return value** 741 742| Type| Description| 743| -------- | -------- | 744| boolean | Returns **true** if the value exists; returns **false** otherwise.| 745 746**Example** 747 748```js 749let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 750let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1)); 751paramsObject.has('bard') === true; 752``` 753 754 755### set<sup>(deprecated)</sup> 756 757set(name: string, value: string): void 758 759Sets 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. 760 761> **NOTE** 762> 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 765**System capability**: SystemCapability.Utils.Lang 766 767**Parameters** 768 769| Name| Type| Mandatory| Description| 770| -------- | -------- | -------- | -------- | 771| name | string | Yes| Key of the value to set.| 772| value | string | Yes| Value to set.| 773 774**Example** 775 776```js 777let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 778let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1)); 779paramsObject.set('baz', '3'); // Add a third parameter. 780``` 781 782 783### sort<sup>(deprecated)</sup> 784 785sort(): void 786 787Sorts 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. 788 789> **NOTE** 790> 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 793**System capability**: SystemCapability.Utils.Lang 794 795**Example** 796 797```js 798let searchParamsObject = new Url.URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object 799searchParamsObject.sort(); // Sort the key/value pairs 800console.log(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=2&c=3&d=4 801``` 802 803 804### keys<sup>(deprecated)</sup> 805 806keys(): IterableIterator<string> 807 808Obtains an ES6 iterator that contains the keys of all the key-value pairs. 809 810> **NOTE** 811> 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 814**System capability**: SystemCapability.Utils.Lang 815 816**Return value** 817 818| Type| Description| 819| -------- | -------- | 820| IterableIterator<string> | ES6 iterator that contains the keys of all the key-value pairs.| 821 822**Example** 823 824```js 825let searchParamsObject = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 826for (var key of searchParamsObject .keys()) { // Output key-value pairs 827 console.log(key); 828} 829``` 830 831 832### values<sup>(deprecated)</sup> 833 834values(): IterableIterator<string> 835 836Obtains an ES6 iterator that contains the values of all the key-value pairs. 837 838> **NOTE** 839> 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 842**System capability**: SystemCapability.Utils.Lang 843 844**Return value** 845 846| Type| Description| 847| -------- | -------- | 848| IterableIterator<string> | ES6 iterator that contains the values of all the key-value pairs.| 849 850**Example** 851 852```js 853let searchParams = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 854for (var value of searchParams.values()) { 855 console.log(value); 856} 857``` 858 859 860### [Symbol.iterator]<sup>(deprecated)</sup> 861 862[Symbol.iterator]\(): IterableIterator<[string, string]> 863 864Obtains 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. 865 866> **NOTE** 867> 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 870**System capability**: SystemCapability.Utils.Lang 871 872**Return value** 873 874| Type| Description| 875| -------- | -------- | 876| IterableIterator<[string, string]> | ES6 iterator.| 877 878**Example** 879 880```js 881const paramsObject = new Url.URLSearchParams('fod=bay&edg=bap'); 882for (const [name, value] of paramsObject) { 883 console.log(name, value); 884} 885``` 886 887### tostring<sup>(deprecated)</sup> 888 889toString(): string 890 891Obtains search parameters that are serialized as a string and, if necessary, percent-encodes the characters in the string. 892 893> **NOTE** 894> 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 897**System capability**: SystemCapability.Utils.Lang 898 899**Return value** 900 901| Type| Description| 902| -------- | -------- | 903| string | String of serialized search parameters, which is percent-encoded if necessary.| 904 905**Example** 906 907```js 908let url = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 909let params = new Url.URLSearchParams(url.search.slice(1)); 910params.append('fod', '3'); 911console.log(params.toString()); 912``` 913 914