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

3
> **NOTE**<br>
W
wusongqing 已提交
4
> 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 已提交
5

W
wusongqing 已提交
6 7

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

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

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


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

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

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

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

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

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

**Example**
Z
zengyawen 已提交
31

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


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

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

Appends a key-value pair into the query string.

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

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

51 52 53 54
 | 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 已提交
55 56

**Example**
W
wusongqing 已提交
57

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


### delete

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

Deletes key-value pairs of the specified key.

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

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

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

**Example**
W
wusongqing 已提交
80

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


### getAll

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

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

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

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

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

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

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

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

110
```js
S
shikai-123 已提交
111 112 113 114
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 已提交
115
```
W
wusongqing 已提交
116 117 118 119


### entries

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

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 已提交
124 125
**System capability**: SystemCapability.Utils.Lang

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

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

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

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


### forEach

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

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

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

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

152 153 154 155
 | 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 已提交
156 157 158

**Table 1** callbackfn parameter description

159 160 161 162 163
 | 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 已提交
164 165 166

**Example**

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


### get

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

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

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

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

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

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

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

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

198
```js
199
var paramsOject = new Url.URLSearchParams(document.location.search.substring(1)); 
W
wusongqing 已提交
200 201 202 203
var name = paramsOject.get("name"); // is the string "Jonathan" 
var age = parseInt(paramsOject.get("age"), 10); // is the number 18
var address = paramsOject.get("address"); // null
```
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**

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

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

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));
W
wusongqing 已提交
255 256
paramsObject.set('baz', 3); // Add a third parameter.
```
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
var 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

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
var 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**

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
var searchParams = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
W
wusongqing 已提交
318 319 320 321
for (var value of searchParams.values()) { 
    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

334 335 336
 | Type | Description |
 | -------- | -------- |
 | IterableIterator&lt;[string,&nbsp;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');
W
wusongqing 已提交
342 343 344 345
for (const [name, value] of paramsObject) { 
    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

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)); 
W
wusongqing 已提交
367 368 369
params.append('fod', 3);
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

404 405 406 407
 | Name | Type | Mandatory | Description |
 | -------- | -------- | -------- | -------- |
 | url | string | Yes | Input object. |
 | base | string&nbsp;\ |&nbsp;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
W
wusongqing 已提交
412
var mm = 'http://username:password@host:8080';
413 414 415 416 417 418 419 420 421 422 423
var a = new Url.URL("/", mm); // Output 'http://username:password@host:8080/';
var b = new Url.URL(mm); // Output 'http://username:password@host:8080/';
new Url.URL('path/path1', b); // Output 'http://username:password@host:8080/path/path1';
var c = new Url.URL('/path/path1', b);  // Output 'http://username:password@host:8080/path/path1'; 
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

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');
W
wusongqing 已提交
445 446
url.toString()
```
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

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');
W
wusongqing 已提交
466
url.toJSON()
467
```