js-apis-uri.md 3.6 KB
Newer Older
W
wusongqing 已提交
1
# URI String Parsing
Z
zengyawen 已提交
2

W
wusongqing 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
W
wusongqing 已提交
4
> The initial APIs of this module are supported since API version 8. 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 uri from '@ohos.uri'  
```

W
wusongqing 已提交
13 14 15 16
## URI

### Attributes

W
wusongqing 已提交
17 18
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| scheme | string | Yes| No| Scheme in the URI.|
| userInfo | string | Yes| No| User information in the URI.|
| host | string | Yes| No| Host name (without the port number) in the URI.|
| port | string | Yes| No| Port number in the URI.|
| path | string | Yes| No| Path in the URI.|
| query | string | Yes| No| Query part in the URI.|
| fragment | string | Yes| No| Fragment part in the URI.|
| authority | string | Yes| No| Authority part in the URI.|
| ssp | string | Yes| No| Scheme-specific part in the URI.|


### constructor

constructor(uri: string)
Z
zengyawen 已提交
35 36 37

A constructor used to create a URI instance.

W
wusongqing 已提交
38 39
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
40
**Parameters**
Z
zengyawen 已提交
41

W
wusongqing 已提交
42 43 44
| Name| Type.| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| url | string | Yes| Yes| Input object.|
Z
zengyawen 已提交
45

W
wusongqing 已提交
46
**Example**
Z
zengyawen 已提交
47

48
```js
W
wusongqing 已提交
49 50 51
var mm = 'http://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
new uri.URI(mm); // Output 'http://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
```
52
```js
W
wusongqing 已提交
53 54 55 56 57
new uri.URI('http://username:password@host:8080'); // Output 'http://username:password@host:8080';
```


### toString
Z
zengyawen 已提交
58

W
wusongqing 已提交
59
toString(): string
Z
zengyawen 已提交
60

W
wusongqing 已提交
61 62
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
63 64 65 66 67 68 69
Obtains the query string applicable to this URL.

**Return value**

| Type.| Description|
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
70

W
wusongqing 已提交
71
**Example**
Z
zengyawen 已提交
72

73
```js
W
wusongqing 已提交
74 75 76
const url = new uri.URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
url.toString()
```
Z
zengyawen 已提交
77 78


W
wusongqing 已提交
79 80 81
### equals

equals(other: URI): boolean
Z
zengyawen 已提交
82 83 84

Checks whether this URI is the same as another URI object.

W
wusongqing 已提交
85 86
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
87
**Parameters**
Z
zengyawen 已提交
88

W
wusongqing 已提交
89 90 91 92 93 94 95 96 97 98 99 100
| Name| Type.| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| other | [URI](#uri) | Yes| URI object to compare.|

**Return value**

| Type.| Description|
| -------- | -------- |
| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|

**Example**

101
```js
W
wusongqing 已提交
102 103 104 105 106 107 108 109
const uriInstance = new uri.URI('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('http://username:password@host:8080/directory/file?query=pppppp#qwer=da#fragment');
uriInstance.equals(uriInstance1);
```

### checkIsAbsolute

checkIsAbsolute(): boolean
Z
zengyawen 已提交
110

W
wusongqing 已提交
111
Checks whether this URI is an absolute URI (whether the scheme component is defined).
Z
zengyawen 已提交
112

W
wusongqing 已提交
113 114
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
115
**Return value**
Z
zengyawen 已提交
116

W
wusongqing 已提交
117 118 119
| Type.| Description|
| -------- | -------- |
| boolean | Returns **true** if the URI is an absolute URI; returns **false** otherwise.|
Z
zengyawen 已提交
120

W
wusongqing 已提交
121
**Example**
Z
zengyawen 已提交
122

123
```js
W
wusongqing 已提交
124 125 126 127 128 129 130 131
const uriInstance = new uri.URI('http://username:password@www.qwer.com:8080?query=pppppp');
uriInstance.checkIsAbsolute();
```


### normalize

normalize(): URI
Z
zengyawen 已提交
132

W
wusongqing 已提交
133 134
Normalizes the path of this URI.

W
wusongqing 已提交
135 136
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
137 138 139 140 141
**Return value**

| Type.| Description|
| -------- | -------- |
| URI | URI with the normalized path.|
Z
zengyawen 已提交
142

W
wusongqing 已提交
143
**Example**
144
```js
W
wusongqing 已提交
145 146 147 148
const uriInstance = new uri.URI('http://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
let uriInstance1 = uriInstance.normalize();
uriInstance1.path;
```