js-apis-url.md 12.9 KB
Newer Older
W
wusongqing 已提交
1
# URL String Parsing
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

W
wusongqing 已提交
7 8

## Modules to Import
Z
zengyawen 已提交
9 10 11 12 13

```
import Url from '@ohos.url' 
```

W
wusongqing 已提交
14
## URLSearchParams
Z
zengyawen 已提交
15 16


W
wusongqing 已提交
17
### constructor
Z
zengyawen 已提交
18

W
wusongqing 已提交
19
constructor(init?: string[][] | Record<string, string> | string | URLSearchParams)
Z
zengyawen 已提交
20

W
wusongqing 已提交
21
Creates a **URLSearchParams** instance.
Z
zengyawen 已提交
22

W
wusongqing 已提交
23 24
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
25 26 27 28
**Parameters**

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

**Example**
Z
zengyawen 已提交
32

33
```js
34 35 36 37 38
let objectParams = new Url.URLSearchParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]);
let objectParams1 = new Url.URLSearchParams({"fod" : '1' , "bard" : '2'});
let objectParams2 = new Url.URLSearchParams('?fod=1&bard=2');
let urlObject = new Url.URL('https://developer.mozilla.org/?fod=1&bard=2');
let params = new Url.URLSearchParams(urlObject.search);
W
wusongqing 已提交
39
```
Z
zengyawen 已提交
40 41


W
wusongqing 已提交
42
### append
Z
zengyawen 已提交
43

W
wusongqing 已提交
44
append(name: string, value: string): void
Z
zengyawen 已提交
45 46 47

Appends a key-value pair into the query string.

W
wusongqing 已提交
48 49
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
50 51
**Parameters**

W
wusongqing 已提交
52 53 54 55
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the key-value pair to append.|
| value | string | Yes| Value of the key-value pair to append.|
W
wusongqing 已提交
56 57

**Example**
W
wusongqing 已提交
58

59
```js
60 61
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1));
S
shikai-123 已提交
62
paramsObject.append('fod', '3');
W
wusongqing 已提交
63
```
W
wusongqing 已提交
64 65 66 67 68


### delete

delete(name: string): void
Z
zengyawen 已提交
69 70 71

Deletes key-value pairs of the specified key.

W
wusongqing 已提交
72 73
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
74 75
**Parameters**

W
wusongqing 已提交
76 77 78
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the key-value pairs to delete.|
W
wusongqing 已提交
79 80

**Example**
W
wusongqing 已提交
81

82
```js
83 84
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsobject = new Url.URLSearchParams(urlObject.search.slice(1));
W
wusongqing 已提交
85
paramsobject.delete('fod');
W
wusongqing 已提交
86
```
W
wusongqing 已提交
87 88 89 90 91


### getAll

getAll(name: string): string[]
Z
zengyawen 已提交
92 93 94

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

W
wusongqing 已提交
95 96
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
97 98
**Parameters**

W
wusongqing 已提交
99 100 101
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain all key-value pairs.|
W
wusongqing 已提交
102 103

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

W
wusongqing 已提交
105 106 107
| Type| Description|
| -------- | -------- |
| string[] | All key-value pairs matching the specified key.|
W
wusongqing 已提交
108

W
wusongqing 已提交
109 110
**Example**

111
```js
S
shikai-123 已提交
112 113 114 115
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new Url.URLSearchParams(urlObject.search.slice(1));
params.append('fod', '3'); // Add a second value for the fod parameter.
console.log(params.getAll('fod').toString()) // Output ["1","3"].
W
wusongqing 已提交
116
```
W
wusongqing 已提交
117 118 119 120


### entries

X
xdmal 已提交
121
entries(): IterableIterator<[string, string]>
W
wusongqing 已提交
122 123 124

Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value respectively.

W
wusongqing 已提交
125 126
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
127 128
**Return value**

W
wusongqing 已提交
129 130
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
131
| IterableIterator&lt;[string, string]&gt; | ES6 iterator.|
W
wusongqing 已提交
132

W
wusongqing 已提交
133 134
**Example**

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


### forEach

X
xdmal 已提交
145
forEach(callbackfn: (value: string, key: string, searchParams: this) => void, thisArg?: Object): void
W
wusongqing 已提交
146 147 148

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

W
wusongqing 已提交
149 150
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
151 152
**Parameters**

W
wusongqing 已提交
153 154 155 156
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the key-value pairs in the **URLSearchParams** instance.|
| thisArg | Object | No| Value to use when the callback is invoked.|
W
wusongqing 已提交
157 158 159

**Table 1** callbackfn parameter description

W
wusongqing 已提交
160 161 162 163 164
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | string | Yes| Value that is currently traversed.|
| key | string | Yes| Key that is currently traversed.|
| searchParams | Object | Yes| Instance that invokes the **forEach** method.|
W
wusongqing 已提交
165 166 167

**Example**

168
```js
169
const myURLObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 
W
wusongqing 已提交
170 171 172 173
myURLObject.searchParams.forEach((value, name, searchParams) => {  
    console.log(name, value, myURLObject.searchParams === searchParams);
});
```
W
wusongqing 已提交
174 175 176 177 178


### get

get(name: string): string | null
Z
zengyawen 已提交
179 180 181

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

W
wusongqing 已提交
182 183
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
184 185
**Parameters**

W
wusongqing 已提交
186 187 188
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain the value.|
W
wusongqing 已提交
189 190

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

W
wusongqing 已提交
192 193 194
| Type| Description|
| -------- | -------- |
| string | Returns the value of the first key-value pair if obtained.|
G
Gloria 已提交
195
| null | Returns **null** if no value is obtained.|
W
wusongqing 已提交
196

W
wusongqing 已提交
197 198
**Example**

199
```js
Z
zengyawen 已提交
200 201 202
let paramsObject = new Url.URLSearchParams('name=Jonathan&age=18'); 
let name = paramsObject.get("name"); // is the string "Jonathan" 
let age = parseInt(paramsObject.get("age"), 10); // is the number 18
W
wusongqing 已提交
203
```
W
wusongqing 已提交
204 205 206 207 208


### has

has(name: string): boolean
Z
zengyawen 已提交
209 210

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

W
wusongqing 已提交
212 213
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
214 215
**Parameters**

W
wusongqing 已提交
216 217 218
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to search for its value.|
W
wusongqing 已提交
219

W
wusongqing 已提交
220 221
**Return value**

W
wusongqing 已提交
222 223 224
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the value exists; returns **false** otherwise.|
W
wusongqing 已提交
225 226 227

**Example**

228
```js
229 230
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1)); 
W
wusongqing 已提交
231 232
paramsObject.has('bard') === true;
```
W
wusongqing 已提交
233 234 235


### set
Z
zengyawen 已提交
236

W
wusongqing 已提交
237
set(name: string, value: string): void
Z
zengyawen 已提交
238 239 240

Sets the value for a key. If key-value pairs matching the specified key exist, the value of the first key-value pair will be set to the specified value and other key-value pairs will be deleted. Otherwise, the key-value pair will be appended to the query string.

W
wusongqing 已提交
241 242
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
243 244
**Parameters**

W
wusongqing 已提交
245 246 247 248
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the value to set.|
| value | string | Yes| Value to set.|
W
wusongqing 已提交
249

W
wusongqing 已提交
250 251
**Example**

252
```js
253 254
let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1));
S
shikai-123 已提交
255
paramsObject.set('baz', '3'); // Add a third parameter.
W
wusongqing 已提交
256
```
W
wusongqing 已提交
257 258 259 260 261


### sort

sort(): void
Z
zengyawen 已提交
262

W
wusongqing 已提交
263
Sorts all key-value pairs contained in this object based on the Unicode code points of the keys and returns undefined.  This method uses a stable sorting algorithm, that is, the relative order between key-value pairs with equal keys is retained.
Z
zengyawen 已提交
264

W
wusongqing 已提交
265
**System capability**: SystemCapability.Utils.Lang
Z
zengyawen 已提交
266

W
wusongqing 已提交
267
**Example**
Z
zengyawen 已提交
268

269
```js
270
let searchParamsObject = new Url.URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object
W
wusongqing 已提交
271 272 273
searchParamsObject.sort(); // Sort the key/value pairs
console.log(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=2&c=3&d=4
```
Z
zengyawen 已提交
274 275


W
wusongqing 已提交
276
### keys
Z
zengyawen 已提交
277

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

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

W
wusongqing 已提交
282 283
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
284
**Return value**
W
wusongqing 已提交
285

W
wusongqing 已提交
286 287 288
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;string&gt; | ES6 iterator that contains the keys of all the key-value pairs.|
W
wusongqing 已提交
289

W
wusongqing 已提交
290
**Example**
W
wusongqing 已提交
291

292
```js
293
let searchParamsObject = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
W
wusongqing 已提交
294 295 296 297
for (var key of searchParamsObject .keys()) { // Output key-value pairs
    console.log(key);
}
```
W
wusongqing 已提交
298 299 300 301 302


### values

values(): IterableIterator&lt;string&gt;
Z
zengyawen 已提交
303 304 305

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

W
wusongqing 已提交
306 307
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
308 309
**Return value**

W
wusongqing 已提交
310 311 312
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;string&gt; | ES6 iterator that contains the values of all the key-value pairs.|
W
wusongqing 已提交
313

W
wusongqing 已提交
314 315
**Example**

316
```js
317 318
let searchParams = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
for (var value of searchParams.values()) {
W
wusongqing 已提交
319 320 321
    console.log(value);
}
```
W
wusongqing 已提交
322 323 324 325


### [Symbol.iterator]

W
wusongqing 已提交
326
[Symbol.iterator]\(): IterableIterator&lt;[string, string]&gt;
W
wusongqing 已提交
327 328 329

Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value respectively.

W
wusongqing 已提交
330 331
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
332
**Return value**
W
wusongqing 已提交
333

W
wusongqing 已提交
334 335
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
336
| IterableIterator&lt;[string, string]&gt; | ES6 iterator.|
W
wusongqing 已提交
337

W
wusongqing 已提交
338
**Example**
W
wusongqing 已提交
339

340
```js
341
const paramsObject = new Url.URLSearchParams('fod=bay&edg=bap');
342
for (const [name, value] of paramsObject) {
W
wusongqing 已提交
343 344 345
    console.log(name, value); 
} 
```
W
wusongqing 已提交
346 347 348 349 350 351 352 353


### tostring

toString(): string

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

W
wusongqing 已提交
354 355
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
356
**Return value**
W
wusongqing 已提交
357

W
wusongqing 已提交
358 359 360
| Type| Description|
| -------- | -------- |
| string | String of serialized search parameters, which is percent-encoded if necessary.|
W
wusongqing 已提交
361

W
wusongqing 已提交
362
**Example**
W
wusongqing 已提交
363

364
```js
365 366
let url = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new Url.URLSearchParams(url.search.slice(1)); 
S
shikai-123 已提交
367
params.append('fod', '3');
W
wusongqing 已提交
368 369
console.log(params.toString());
```
W
wusongqing 已提交
370 371 372 373 374 375


## URL

### Attributes

W
wusongqing 已提交
376 377
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| hash | string | Yes| Yes| String that contains a harsh mark (#) followed by the fragment identifier of a URL.|
| host | string | Yes| Yes| Host information in a URL.|
| hostname | string | Yes| Yes| Hostname (without the port) in a URL.|
| href | string | Yes| Yes| String that contains the whole URL.|
| origin | string | Yes| No| Read-only string that contains the Unicode serialization of the origin of the represented URL.|
| password | string | Yes| Yes| Password in a URL.|
| pathname | string | Yes| Yes| Path in a URL.|
| port | string | Yes| Yes| Port in a URL.|
| protocol | string | Yes| Yes| Protocol in a URL.|
| search | string | Yes| Yes| Serialized query string in a URL.|
| searchParams | URLsearchParams | Yes| No| **URLSearchParams** object allowing access to the query parameters in a URL.|
| username | string | Yes| Yes| Username in a URL.|


### constructor

constructor(url: string, base?: string | URL)
Z
zengyawen 已提交
397 398 399

Creates a URL.

W
wusongqing 已提交
400 401
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
402
**Parameters**
Z
zengyawen 已提交
403

W
wusongqing 已提交
404 405 406
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| url | string | Yes| Input object.|
G
Gloria 已提交
407
| base | string \| URL | No| Input parameter, which can be any of the following:<br>- **string**: string<br>- **URL**: string or object|
W
wusongqing 已提交
408

W
wusongqing 已提交
409
**Example**
Z
zengyawen 已提交
410

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


W
wusongqing 已提交
427
### tostring
Z
zengyawen 已提交
428

W
wusongqing 已提交
429
toString(): string
Z
zengyawen 已提交
430

W
wusongqing 已提交
431
Converts the parsed URL into a string.
Z
zengyawen 已提交
432

W
wusongqing 已提交
433 434
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
435
**Return value**
Z
zengyawen 已提交
436

W
wusongqing 已提交
437 438 439
| Type| Description|
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
440

W
wusongqing 已提交
441
**Example**
Z
zengyawen 已提交
442

443
```js
444
const url = new Url.URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
G
Gloria 已提交
445
url.toString();
W
wusongqing 已提交
446
```
W
wusongqing 已提交
447

Z
zengyawen 已提交
448

W
wusongqing 已提交
449
### toJSON
Z
zengyawen 已提交
450

W
wusongqing 已提交
451
toJSON(): string
Z
zengyawen 已提交
452

W
wusongqing 已提交
453 454
Converts the parsed URL into a JSON string.

W
wusongqing 已提交
455 456
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
457
**Return value**
Z
zengyawen 已提交
458

W
wusongqing 已提交
459 460 461
| Type| Description|
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
462

W
wusongqing 已提交
463
**Example**
464
```js
465
const url = new Url.URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
G
Gloria 已提交
466
url.toJSON();
W
wusongqing 已提交
467
```