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