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

W
wusongqing 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
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
## System Capabilities
W
wusongqing 已提交
14

W
wusongqing 已提交
15
SystemCapability.Utils.Lang
Z
zengyawen 已提交
16

W
wusongqing 已提交
17
## URLSearchParams
Z
zengyawen 已提交
18 19


W
wusongqing 已提交
20
### constructor
Z
zengyawen 已提交
21

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

W
wusongqing 已提交
24
Creates a **URLSearchParams** instance.
Z
zengyawen 已提交
25

W
wusongqing 已提交
26 27 28 29 30 31 32
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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/>-&nbsp;**string[][]**: two-dimensional string array<br/>-&nbsp;**Record&lt;string,&nbsp;string&gt;**: list of objects<br/>-&nbsp;**string**: string<br/>-&nbsp;**URLSearchParams**: object|

**Example**
Z
zengyawen 已提交
33

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


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

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

Appends a key-value pair into the query string.

W
wusongqing 已提交
49 50 51 52 53 54 55 56
**Parameters**

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

**Example**
W
wusongqing 已提交
57

W
wusongqing 已提交
58 59 60 61 62
```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new URLSearchParams(urlObject.search.slice(1));
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 73 74 75 76 77
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the key-value pairs to delete.|

**Example**
W
wusongqing 已提交
78

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


### getAll

getAll(name: string): string[]
Z
zengyawen 已提交
89 90 91

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

W
wusongqing 已提交
92 93 94 95 96 97 98
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain all key-value pairs.|

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

W
wusongqing 已提交
100 101 102
| Type| Description|
| -------- | -------- |
| string[] | All key-value pairs matching the specified key.|
W
wusongqing 已提交
103

W
wusongqing 已提交
104 105 106 107 108 109 110 111
**Example**

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


### entries

entries(): IterableIterator&lt;[string, string]&gt;

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 已提交
120 121 122 123 124
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;[string,&nbsp;string]&gt; | An ES6 iterator.|
W
wusongqing 已提交
125

W
wusongqing 已提交
126 127 128 129 130 131 132 133
**Example**

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


### forEach

forEach(callbackfn: (value: string, key: string, searchParams: Object) =&gt; void, thisArg?: Object): void

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

W
wusongqing 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
**Parameters**

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

**Table 1** callbackfn parameter description

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

**Example**

```
const myURLObject = new URL('https://developer.exampleUrl/?fod=1&bard=2'); 
myURLObject.searchParams.forEach((value, name, searchParams) => {  
    console.log(name, value, myURLObject.searchParams === searchParams);
});
```
W
wusongqing 已提交
165 166 167 168 169


### get

get(name: string): string | null
Z
zengyawen 已提交
170 171 172

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

W
wusongqing 已提交
173 174 175 176 177 178 179
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key specified to obtain the value.|

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

W
wusongqing 已提交
181 182 183 184
| Type| Description|
| -------- | -------- |
| string | Returns the value of the first key-value pair if obtained.|
| null | Returns null if no value is obtained.|
W
wusongqing 已提交
185

W
wusongqing 已提交
186 187 188 189 190 191 192 193
**Example**

```
var paramsOject = new URLSearchParams(document.location.search.substring(1)); 
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 已提交
194 195 196 197 198


### has

has(name: string): boolean
Z
zengyawen 已提交
199 200

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

W
wusongqing 已提交
202 203 204 205 206
**Parameters**

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

W
wusongqing 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220
**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the value exists; returns **false** otherwise.|

**Example**

```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new URLSearchParams(urlObject.search.slice(1)); 
paramsObject.has('bard') === true;
```
W
wusongqing 已提交
221 222 223


### set
Z
zengyawen 已提交
224

W
wusongqing 已提交
225
set(name: string, value: string): void
Z
zengyawen 已提交
226 227 228

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 已提交
229 230 231 232 233 234
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Key of the value to set.|
| value | string | Yes| Value to set.|
W
wusongqing 已提交
235

W
wusongqing 已提交
236 237 238 239 240 241 242
**Example**

```
let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new URLSearchParams(urlObject.search.slice(1));
paramsObject.set('baz', 3); // Add a third parameter.
```
W
wusongqing 已提交
243 244 245 246 247


### sort

sort(): void
Z
zengyawen 已提交
248 249


W
wusongqing 已提交
250
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 已提交
251

W
wusongqing 已提交
252
**Example**
Z
zengyawen 已提交
253

W
wusongqing 已提交
254 255 256 257 258
```
var searchParamsObject = new URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object
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 已提交
259 260


W
wusongqing 已提交
261
### keys
Z
zengyawen 已提交
262

W
wusongqing 已提交
263
keys(): IterableIterator&lt;string&gt;
Z
zengyawen 已提交
264 265


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

W
wusongqing 已提交
268
**Return value**
W
wusongqing 已提交
269

W
wusongqing 已提交
270 271 272
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;string&gt; | ES6 iterator that contains the keys of all the key-value pairs.|
W
wusongqing 已提交
273

W
wusongqing 已提交
274
**Example**
W
wusongqing 已提交
275

W
wusongqing 已提交
276 277 278 279 280 281
```
var searchParamsObject = new URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
for (var key of searchParamsObject .keys()) { // Output key-value pairs
    console.log(key);
}
```
W
wusongqing 已提交
282 283 284 285 286


### values

values(): IterableIterator&lt;string&gt;
Z
zengyawen 已提交
287 288 289

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

W
wusongqing 已提交
290 291 292 293 294
**Return value**

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

W
wusongqing 已提交
296 297 298 299 300 301 302 303
**Example**

```
var searchParams = new URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
for (var value of searchParams.values()) { 
    console.log(value);
}
```
W
wusongqing 已提交
304 305 306 307


### [Symbol.iterator]

W
wusongqing 已提交
308
[Symbol.iterator]\(): IterableIterator&lt;[string, string]&gt;
W
wusongqing 已提交
309 310 311 312


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 已提交
313
**Return value**
W
wusongqing 已提交
314

W
wusongqing 已提交
315 316 317
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;[string,&nbsp;string]&gt; | ES6 iterator.|
W
wusongqing 已提交
318

W
wusongqing 已提交
319
**Example**
W
wusongqing 已提交
320

W
wusongqing 已提交
321 322 323 324 325 326
```
const paramsObject = new URLSearchParams('fod=bay&edg=bap');
for (const [name, value] of paramsObject) { 
    console.log(name, value); 
} 
```
W
wusongqing 已提交
327 328 329 330 331 332 333 334 335


### tostring

toString(): string


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

W
wusongqing 已提交
336
**Return value**
W
wusongqing 已提交
337

W
wusongqing 已提交
338 339 340
| Type| Description|
| -------- | -------- |
| string | String of serialized search parameters, which is percent-encoded if necessary.|
W
wusongqing 已提交
341

W
wusongqing 已提交
342
**Example**
W
wusongqing 已提交
343

W
wusongqing 已提交
344 345 346 347 348 349
```
let url = new URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new URLSearchParams(url.search.slice(1)); 
params.append('fod', 3);
console.log(params.toString());
```
W
wusongqing 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375


## URL


### Attributes

| 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 已提交
376 377 378 379


Creates a URL.

W
wusongqing 已提交
380
**Parameters**
Z
zengyawen 已提交
381

W
wusongqing 已提交
382 383 384 385
| 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/>-&nbsp;**string**: string<br/>-&nbsp;**URL**: string or object|
W
wusongqing 已提交
386

W
wusongqing 已提交
387
**Example**
Z
zengyawen 已提交
388

W
wusongqing 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402
```
var mm = 'http://username:password@host:8080';
var a = new URL("/", mm); // Output 'http://username:password@host:8080/';
var b = new URL(mm); // Output 'http://username:password@host:8080/';
new URL('path/path1', b); // Output 'http://username:password@host:8080/path/path1';
var c = new URL('/path/path1', b);  // Output 'http://username:password@host:8080/path/path1'; 
new URL('/path/path1', c); // Output 'http://username:password@host:8080/path/path1';
new URL('/path/path1', a); // Output 'http://username:password@host:8080/path/path1';
new URL('/path/path1', "https://www.exampleUrl/fr-FR/toto"); // Output https://www.exampleUrl/path/path1
new URL('/path/path1', ''); // Raises a TypeError exception as '' is not a valid URL
new URL('/path/path1'); // Raises a TypeError exception as '/path/path1' is not a valid URL
new URL('http://www.shanxi.com', ); // Output http://www.shanxi.com/
new URL('http://www.shanxi.com', b); // Output http://www.shanxi.com/
```
Z
zengyawen 已提交
403 404


W
wusongqing 已提交
405
### tostring
Z
zengyawen 已提交
406

W
wusongqing 已提交
407
toString(): string
Z
zengyawen 已提交
408

W
wusongqing 已提交
409
Converts the parsed URL into a string.
Z
zengyawen 已提交
410

W
wusongqing 已提交
411
**Return value**
Z
zengyawen 已提交
412

W
wusongqing 已提交
413 414 415
| Type| Description|
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
416

W
wusongqing 已提交
417
**Example**
Z
zengyawen 已提交
418

W
wusongqing 已提交
419 420 421 422
```
const url = new URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
url.toString()
```
W
wusongqing 已提交
423

Z
zengyawen 已提交
424

W
wusongqing 已提交
425
### toJSON
Z
zengyawen 已提交
426

W
wusongqing 已提交
427
toJSON(): string
Z
zengyawen 已提交
428 429


W
wusongqing 已提交
430 431
Converts the parsed URL into a JSON string.

W
wusongqing 已提交
432
**Return value**
Z
zengyawen 已提交
433

W
wusongqing 已提交
434 435 436
| Type| Description|
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
437

W
wusongqing 已提交
438 439 440 441 442
**Example**
```
const url = new URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
url.toJSON()
```