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