js-apis-uri.md 4.5 KB
Newer Older
G
Gloria 已提交
1
# @ohos.uri (URI String Parsing)
Z
zengyawen 已提交
2

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

W
wusongqing 已提交
7 8

## Modules to Import
Z
zengyawen 已提交
9

W
wusongqing 已提交
10
```js
Z
zengyawen 已提交
11 12 13
import uri from '@ohos.uri'  
```

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

### Attributes

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

W
wusongqing 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| 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 已提交
36 37 38

A constructor used to create a URI instance.

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

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

G
Gloria 已提交
43 44 45
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| uri | string | Yes| Input object.|
Z
zengyawen 已提交
46

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

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


### toString
Z
zengyawen 已提交
59

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

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

W
wusongqing 已提交
64
Obtains the query string applicable to this URI.
W
wusongqing 已提交
65 66 67

**Return value**

G
Gloria 已提交
68
| Type| Description|
W
wusongqing 已提交
69 70
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
71

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

74
```js
G
Gloria 已提交
75
const result = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
S
shikai-123 已提交
76
result.toString()
W
wusongqing 已提交
77
```
Z
zengyawen 已提交
78 79


G
Gloria 已提交
80
### equals<sup>(deprecated)</sup>
G
Gloria 已提交
81

G
Gloria 已提交
82 83
> **NOTE**
>
G
Gloria 已提交
84
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [equalsTo<sup>9+</sup>](#equalsto9) instead.
W
wusongqing 已提交
85 86

equals(other: URI): boolean
Z
zengyawen 已提交
87 88 89

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

W
wusongqing 已提交
90 91
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
92
**Parameters**
Z
zengyawen 已提交
93

G
Gloria 已提交
94
| Name| Type| Mandatory| Description|
W
wusongqing 已提交
95 96 97 98 99
| -------- | -------- | -------- | -------- |
| other | [URI](#uri) | Yes| URI object to compare.|

**Return value**

G
Gloria 已提交
100
| Type| Description|
W
wusongqing 已提交
101 102 103 104 105
| -------- | -------- |
| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|

**Example**

106
```js
G
Gloria 已提交
107 108
const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da#fragment');
W
wusongqing 已提交
109 110
uriInstance.equals(uriInstance1);
```
G
Gloria 已提交
111 112 113 114 115 116 117 118 119 120
### equalsTo<sup>9+</sup>

equalsTo(other: URI): boolean

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

**System capability**: SystemCapability.Utils.Lang

**Parameters**

G
Gloria 已提交
121
| Name| Type| Mandatory| Description|
G
Gloria 已提交
122 123 124 125 126
| -------- | -------- | -------- | -------- |
| other | [URI](#uri) | Yes| URI object to compare.|

**Return value**

G
Gloria 已提交
127
| Type| Description|
G
Gloria 已提交
128 129 130 131 132 133
| -------- | -------- |
| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|

**Example**

```js
G
Gloria 已提交
134 135
const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da#fragment');
G
Gloria 已提交
136 137
uriInstance.equalsTo(uriInstance1);
```
W
wusongqing 已提交
138 139 140 141

### checkIsAbsolute

checkIsAbsolute(): boolean
Z
zengyawen 已提交
142

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

W
wusongqing 已提交
145 146
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
147
**Return value**
Z
zengyawen 已提交
148

G
Gloria 已提交
149
| Type| Description|
W
wusongqing 已提交
150 151
| -------- | -------- |
| boolean | Returns **true** if the URI is an absolute URI; returns **false** otherwise.|
Z
zengyawen 已提交
152

W
wusongqing 已提交
153
**Example**
Z
zengyawen 已提交
154

155
```js
G
Gloria 已提交
156
const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp');
W
wusongqing 已提交
157 158 159 160 161 162 163
uriInstance.checkIsAbsolute();
```


### normalize

normalize(): URI
Z
zengyawen 已提交
164

W
wusongqing 已提交
165 166
Normalizes the path of this URI.

W
wusongqing 已提交
167 168
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
169 170
**Return value**

G
Gloria 已提交
171
| Type| Description|
W
wusongqing 已提交
172 173
| -------- | -------- |
| URI | URI with the normalized path.|
Z
zengyawen 已提交
174

W
wusongqing 已提交
175
**Example**
G
Gloria 已提交
176

177
```js
G
Gloria 已提交
178
const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
W
wusongqing 已提交
179 180 181
let uriInstance1 = uriInstance.normalize();
uriInstance1.path;
```