js-apis-url.md 52.2 KB
Newer Older
Z
zengyawen 已提交
1
# URL字符串解析<a name="ZH-CN_TOPIC_0000001151274864"></a>
Z
zengyawen 已提交
2 3 4 5

>![](../../public_sys-resources/icon-note.gif) **说明:** 
>从 API Version 7 开始支持。

Z
zengyawen 已提交
6
## 导入模块<a name="section2080512344162"></a>
Z
zengyawen 已提交
7 8 9 10 11

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

Z
zengyawen 已提交
12
## 权限<a name="section11257113618419"></a>
Z
zengyawen 已提交
13 14 15



Z
zengyawen 已提交
16
## URLSearchParams<a name="section143808167355"></a>
Z
zengyawen 已提交
17

Z
zengyawen 已提交
18
### constructor<a name="section694710593283"></a>
Z
zengyawen 已提交
19 20 21 22 23 24 25

constructor\(init?: string\[\]\[\] | Record<string, string\> | string | URLSearchParams\)

URLSearchParams的构造函数。

-   参数:

Z
zengyawen 已提交
26 27
    <a name="table1540155452420"></a>
    <table><thead align="left"><tr id="row1947713549244"><th class="cellrowborder" valign="top" width="12.959999999999999%" id="mcps1.1.5.1.1"><p id="p74771754102410"><a name="p74771754102410"></a><a name="p74771754102410"></a>参数名</p>
Z
zengyawen 已提交
28
    </th>
Z
zengyawen 已提交
29
    <th class="cellrowborder" valign="top" width="32.06%" id="mcps1.1.5.1.2"><p id="p1047755412411"><a name="p1047755412411"></a><a name="p1047755412411"></a>类型</p>
Z
zengyawen 已提交
30
    </th>
Z
zengyawen 已提交
31
    <th class="cellrowborder" valign="top" width="6.569999999999999%" id="mcps1.1.5.1.3"><p id="p34782054192412"><a name="p34782054192412"></a><a name="p34782054192412"></a>必填</p>
Z
zengyawen 已提交
32
    </th>
Z
zengyawen 已提交
33
    <th class="cellrowborder" valign="top" width="48.41%" id="mcps1.1.5.1.4"><p id="p12478954132419"><a name="p12478954132419"></a><a name="p12478954132419"></a>说明</p>
Z
zengyawen 已提交
34 35 36
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
37
    <tbody><tr id="row1247811541240"><td class="cellrowborder" valign="top" width="12.959999999999999%" headers="mcps1.1.5.1.1 "><p id="p2089352474715"><a name="p2089352474715"></a><a name="p2089352474715"></a>init</p>
Z
zengyawen 已提交
38
    </td>
Z
zengyawen 已提交
39
    <td class="cellrowborder" valign="top" width="32.06%" headers="mcps1.1.5.1.2 "><p id="p81221923163816"><a name="p81221923163816"></a><a name="p81221923163816"></a>string[][] | Record&lt;string, string&gt; | string | URLSearchParams</p>
Z
zengyawen 已提交
40
    </td>
Z
zengyawen 已提交
41
    <td class="cellrowborder" valign="top" width="6.569999999999999%" headers="mcps1.1.5.1.3 "><p id="p1247885492411"><a name="p1247885492411"></a><a name="p1247885492411"></a>否</p>
Z
zengyawen 已提交
42
    </td>
Z
zengyawen 已提交
43 44
    <td class="cellrowborder" valign="top" width="48.41%" headers="mcps1.1.5.1.4 "><p id="p12478654172415"><a name="p12478654172415"></a><a name="p12478654172415"></a>入参对象。</p>
    <a name="ul11644162993910"></a><a name="ul11644162993910"></a><ul id="ul11644162993910"><li>string[][]:字符串二维数组</li><li>Record&lt;string, string&gt;:对象列表</li><li>string:字符串</li><li>URLSearchParams:对象</li></ul>
Z
zengyawen 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    </td>
    </tr>
    </tbody>
    </table>

-   示例:

    ```
    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 已提交
61
### append<a name="section31006818349"></a>
Z
zengyawen 已提交
62 63 64 65 66 67 68

append\(name: string, value: string\): void

将新的键值对插入到查询字符串。

-   参数:

Z
zengyawen 已提交
69 70
    <a name="table69661135912"></a>
    <table><thead align="left"><tr id="row149668318915"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p7966738914"><a name="p7966738914"></a><a name="p7966738914"></a>参数名</p>
Z
zengyawen 已提交
71
    </th>
Z
zengyawen 已提交
72
    <th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p296713699"><a name="p296713699"></a><a name="p296713699"></a>类型</p>
Z
zengyawen 已提交
73
    </th>
Z
zengyawen 已提交
74
    <th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p196718315911"><a name="p196718315911"></a><a name="p196718315911"></a>必填</p>
Z
zengyawen 已提交
75
    </th>
Z
zengyawen 已提交
76
    <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p9967231197"><a name="p9967231197"></a><a name="p9967231197"></a>说明</p>
Z
zengyawen 已提交
77 78 79
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
80
    <tbody><tr id="row99671533914"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p79671633910"><a name="p79671633910"></a><a name="p79671633910"></a>name</p>
Z
zengyawen 已提交
81
    </td>
Z
zengyawen 已提交
82
    <td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p11967433914"><a name="p11967433914"></a><a name="p11967433914"></a>string</p>
Z
zengyawen 已提交
83
    </td>
Z
zengyawen 已提交
84
    <td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p19671336916"><a name="p19671336916"></a><a name="p19671336916"></a>是</p>
Z
zengyawen 已提交
85
    </td>
Z
zengyawen 已提交
86
    <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p69671631796"><a name="p69671631796"></a><a name="p69671631796"></a>需要插入搜索参数的键名。</p>
Z
zengyawen 已提交
87 88
    </td>
    </tr>
Z
zengyawen 已提交
89
    <tr id="row18967831393"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p39671131590"><a name="p39671131590"></a><a name="p39671131590"></a>value</p>
Z
zengyawen 已提交
90
    </td>
Z
zengyawen 已提交
91
    <td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p126051952172518"><a name="p126051952172518"></a><a name="p126051952172518"></a>string</p>
Z
zengyawen 已提交
92
    </td>
Z
zengyawen 已提交
93
    <td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p149671932919"><a name="p149671932919"></a><a name="p149671932919"></a>是</p>
Z
zengyawen 已提交
94
    </td>
Z
zengyawen 已提交
95
    <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p19675312911"><a name="p19675312911"></a><a name="p19675312911"></a>需要插入搜索参数的值。</p>
Z
zengyawen 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109
    </td>
    </tr>
    </tbody>
    </table>

-   示例:

    ```
    let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
    let paramsObject = new URLSearchParams(urlObject.search.slice(1));
    paramsObject.append('fod', 3);
    ```


Z
zengyawen 已提交
110
### delete<a name="section25256353342"></a>
Z
zengyawen 已提交
111 112 113 114 115 116 117

delete\(name: string\): void

删除指定名称的键值对。

-   参数:

Z
zengyawen 已提交
118 119
    <a name="table06501420123810"></a>
    <table><thead align="left"><tr id="row7651202014381"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p465112013817"><a name="p465112013817"></a><a name="p465112013817"></a>参数名</p>
Z
zengyawen 已提交
120
    </th>
Z
zengyawen 已提交
121
    <th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p1651152053811"><a name="p1651152053811"></a><a name="p1651152053811"></a>类型</p>
Z
zengyawen 已提交
122
    </th>
Z
zengyawen 已提交
123
    <th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p10651102018383"><a name="p10651102018383"></a><a name="p10651102018383"></a>必填</p>
Z
zengyawen 已提交
124
    </th>
Z
zengyawen 已提交
125
    <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p16511220143812"><a name="p16511220143812"></a><a name="p16511220143812"></a>说明</p>
Z
zengyawen 已提交
126 127 128
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
129
    <tbody><tr id="row19651202063816"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p176511020113820"><a name="p176511020113820"></a><a name="p176511020113820"></a>name</p>
Z
zengyawen 已提交
130
    </td>
Z
zengyawen 已提交
131
    <td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p1865152043815"><a name="p1865152043815"></a><a name="p1865152043815"></a>string</p>
Z
zengyawen 已提交
132
    </td>
Z
zengyawen 已提交
133
    <td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p365292023811"><a name="p365292023811"></a><a name="p365292023811"></a>是</p>
Z
zengyawen 已提交
134
    </td>
Z
zengyawen 已提交
135
    <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p4652192063817"><a name="p4652192063817"></a><a name="p4652192063817"></a>需要删除的键值名称。</p>
Z
zengyawen 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

    ```
    let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
    let paramsobject = new URLSearchParams(urlObject.search.slice(1));
    paramsobject.delete('foo');
    ```


Z
zengyawen 已提交
151
### getAll<a name="section2178125553415"></a>
Z
zengyawen 已提交
152 153 154 155 156 157 158

getAll\(name: string\): string\[\]

获取指定名称的所有键值对。

-   参数:

Z
zengyawen 已提交
159 160
    <a name="table344935313917"></a>
    <table><thead align="left"><tr id="row1344915383919"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p134498535393"><a name="p134498535393"></a><a name="p134498535393"></a>参数名</p>
Z
zengyawen 已提交
161
    </th>
Z
zengyawen 已提交
162
    <th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p445055343918"><a name="p445055343918"></a><a name="p445055343918"></a>类型</p>
Z
zengyawen 已提交
163
    </th>
Z
zengyawen 已提交
164
    <th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p17450153173914"><a name="p17450153173914"></a><a name="p17450153173914"></a>必填</p>
Z
zengyawen 已提交
165
    </th>
Z
zengyawen 已提交
166
    <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p1945045312399"><a name="p1945045312399"></a><a name="p1945045312399"></a>说明</p>
Z
zengyawen 已提交
167 168 169
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
170
    <tbody><tr id="row184505536396"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p174501853103913"><a name="p174501853103913"></a><a name="p174501853103913"></a>name</p>
Z
zengyawen 已提交
171
    </td>
Z
zengyawen 已提交
172
    <td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p114501753123918"><a name="p114501753123918"></a><a name="p114501753123918"></a>string</p>
Z
zengyawen 已提交
173
    </td>
Z
zengyawen 已提交
174
    <td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p1245015534394"><a name="p1245015534394"></a><a name="p1245015534394"></a>是</p>
Z
zengyawen 已提交
175
    </td>
Z
zengyawen 已提交
176
    <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p44501153143910"><a name="p44501153143910"></a><a name="p44501153143910"></a>指定的键值名称。</p>
Z
zengyawen 已提交
177 178 179 180 181 182 183 184
    </td>
    </tr>
    </tbody>
    </table>


-   返回值:

Z
zengyawen 已提交
185 186
    <a name="table37864874412"></a>
    <table><thead align="left"><tr id="row1778638104418"><th class="cellrowborder" valign="top" width="20.990000000000002%" id="mcps1.1.3.1.1"><p id="p1778613834415"><a name="p1778613834415"></a><a name="p1778613834415"></a>类型</p>
Z
zengyawen 已提交
187
    </th>
Z
zengyawen 已提交
188
    <th class="cellrowborder" valign="top" width="79.01%" id="mcps1.1.3.1.2"><p id="p6786118124414"><a name="p6786118124414"></a><a name="p6786118124414"></a>说明</p>
Z
zengyawen 已提交
189 190 191
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
192
    <tbody><tr id="row678611818445"><td class="cellrowborder" valign="top" width="20.990000000000002%" headers="mcps1.1.3.1.1 "><p id="p83451276515"><a name="p83451276515"></a><a name="p83451276515"></a>string[]</p>
Z
zengyawen 已提交
193
    </td>
Z
zengyawen 已提交
194
    <td class="cellrowborder" valign="top" width="79.01%" headers="mcps1.1.3.1.2 "><p id="p6786118144419"><a name="p6786118144419"></a><a name="p6786118144419"></a>返回指定名称的所有键值对。</p>
Z
zengyawen 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

    ```
    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"].
    ```


Z
zengyawen 已提交
211
### entries<a name="section139696402356"></a>
Z
zengyawen 已提交
212 213 214 215 216 217 218

entries\(\): IterableIterator<\[string, string\]\>

返回一个ES6的迭代器,迭代器的每一项都是一个 JavaScript Array。Array的第一项是name,Array的第二项是value。

-   返回值:

Z
zengyawen 已提交
219 220
    <a name="table092116152217"></a>
    <table><thead align="left"><tr id="row792256172217"><th class="cellrowborder" valign="top" width="33.660000000000004%" id="mcps1.1.3.1.1"><p id="p392286122211"><a name="p392286122211"></a><a name="p392286122211"></a>类型</p>
Z
zengyawen 已提交
221
    </th>
Z
zengyawen 已提交
222
    <th class="cellrowborder" valign="top" width="66.34%" id="mcps1.1.3.1.2"><p id="p7922463223"><a name="p7922463223"></a><a name="p7922463223"></a>说明</p>
Z
zengyawen 已提交
223 224 225
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
226
    <tbody><tr id="row189221612226"><td class="cellrowborder" valign="top" width="33.660000000000004%" headers="mcps1.1.3.1.1 "><p id="p11940347163112"><a name="p11940347163112"></a><a name="p11940347163112"></a>IterableIterator&lt;[string, string]&gt;</p>
Z
zengyawen 已提交
227
    </td>
Z
zengyawen 已提交
228
    <td class="cellrowborder" valign="top" width="66.34%" headers="mcps1.1.3.1.2 "><p id="p310681516226"><a name="p310681516226"></a><a name="p310681516226"></a>返回一个ES6的迭代器。</p>
Z
zengyawen 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

    ```
    var searchParamsObject = new URLSearchParams("keyName1=valueName1&keyName2=valueName2"); 
    for (var pair of searchParamsObject .entries()) { // Show keyName/valueName pairs
        console.log(pair[0]+ ', '+ pair[1]);
    }
    ```


Z
zengyawen 已提交
245
### forEach<a name="section299942113617"></a>
Z
zengyawen 已提交
246

Z
zengyawen 已提交
247
forEach\(callbackfn: \(value: string, key: string, searchParams: Object\) =\> void, thisArg?: Object\): void
Z
zengyawen 已提交
248 249 250 251 252

通过回调函数来遍历URLSearchParams实例对象上的键值对。

-   参数:

Z
zengyawen 已提交
253 254
    <a name="table127915306330"></a>
    <table><thead align="left"><tr id="row1727910302334"><th class="cellrowborder" valign="top" width="16.43%" id="mcps1.1.5.1.1"><p id="p1237183514330"><a name="p1237183514330"></a><a name="p1237183514330"></a>参数名</p>
Z
zengyawen 已提交
255
    </th>
Z
zengyawen 已提交
256
    <th class="cellrowborder" valign="top" width="16.2%" id="mcps1.1.5.1.2"><p id="p73716356332"><a name="p73716356332"></a><a name="p73716356332"></a>类型</p>
Z
zengyawen 已提交
257
    </th>
Z
zengyawen 已提交
258
    <th class="cellrowborder" valign="top" width="10.14%" id="mcps1.1.5.1.3"><p id="p203711435133319"><a name="p203711435133319"></a><a name="p203711435133319"></a>必填</p>
Z
zengyawen 已提交
259
    </th>
Z
zengyawen 已提交
260
    <th class="cellrowborder" valign="top" width="57.230000000000004%" id="mcps1.1.5.1.4"><p id="p2037111353332"><a name="p2037111353332"></a><a name="p2037111353332"></a>说明</p>
Z
zengyawen 已提交
261 262 263
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
264 265 266 267 268 269 270 271 272 273
    <tbody><tr id="row1728073073310"><td class="cellrowborder" valign="top" width="16.43%" headers="mcps1.1.5.1.1 "><p id="p1728014306336"><a name="p1728014306336"></a><a name="p1728014306336"></a>callbackfn</p>
    </td>
    <td class="cellrowborder" valign="top" width="16.2%" headers="mcps1.1.5.1.2 "><p id="p1928014303336"><a name="p1928014303336"></a><a name="p1928014303336"></a>function</p>
    </td>
    <td class="cellrowborder" valign="top" width="10.14%" headers="mcps1.1.5.1.3 "><p id="p82801930193317"><a name="p82801930193317"></a><a name="p82801930193317"></a>是</p>
    </td>
    <td class="cellrowborder" valign="top" width="57.230000000000004%" headers="mcps1.1.5.1.4 "><p id="p2280133023319"><a name="p2280133023319"></a><a name="p2280133023319"></a>回调函数。</p>
    </td>
    </tr>
    <tr id="row1061141371920"><td class="cellrowborder" valign="top" width="16.43%" headers="mcps1.1.5.1.1 "><p id="p1962171319197"><a name="p1962171319197"></a><a name="p1962171319197"></a>thisArg</p>
Z
zengyawen 已提交
274
    </td>
Z
zengyawen 已提交
275
    <td class="cellrowborder" valign="top" width="16.2%" headers="mcps1.1.5.1.2 "><p id="p56291331918"><a name="p56291331918"></a><a name="p56291331918"></a>Object</p>
Z
zengyawen 已提交
276
    </td>
Z
zengyawen 已提交
277
    <td class="cellrowborder" valign="top" width="10.14%" headers="mcps1.1.5.1.3 "><p id="p136251321915"><a name="p136251321915"></a><a name="p136251321915"></a>否</p>
Z
zengyawen 已提交
278
    </td>
Z
zengyawen 已提交
279
    <td class="cellrowborder" valign="top" width="57.230000000000004%" headers="mcps1.1.5.1.4 "><p id="p16631613131917"><a name="p16631613131917"></a><a name="p16631613131917"></a>callbackfn被调用时用作this值</p>
Z
zengyawen 已提交
280 281 282 283 284 285 286
    </td>
    </tr>
    </tbody>
    </table>

    **表 1**  callbackfn的参数说明

Z
zengyawen 已提交
287 288
    <a name="table11630163925517"></a>
    <table><thead align="left"><tr id="row18630183913556"><th class="cellrowborder" valign="top" width="16.46%" id="mcps1.2.5.1.1"><p id="p6630939115517"><a name="p6630939115517"></a><a name="p6630939115517"></a>参数名</p>
Z
zengyawen 已提交
289
    </th>
Z
zengyawen 已提交
290
    <th class="cellrowborder" valign="top" width="15.8%" id="mcps1.2.5.1.2"><p id="p156301439145520"><a name="p156301439145520"></a><a name="p156301439145520"></a>类型</p>
Z
zengyawen 已提交
291
    </th>
Z
zengyawen 已提交
292
    <th class="cellrowborder" valign="top" width="9.520000000000001%" id="mcps1.2.5.1.3"><p id="p166319395559"><a name="p166319395559"></a><a name="p166319395559"></a>必填</p>
Z
zengyawen 已提交
293
    </th>
Z
zengyawen 已提交
294
    <th class="cellrowborder" valign="top" width="58.220000000000006%" id="mcps1.2.5.1.4"><p id="p206311039115511"><a name="p206311039115511"></a><a name="p206311039115511"></a>说明</p>
Z
zengyawen 已提交
295 296 297
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
298
    <tbody><tr id="row963193918557"><td class="cellrowborder" valign="top" width="16.46%" headers="mcps1.2.5.1.1 "><p id="p16631163925512"><a name="p16631163925512"></a><a name="p16631163925512"></a>value</p>
Z
zengyawen 已提交
299
    </td>
Z
zengyawen 已提交
300
    <td class="cellrowborder" valign="top" width="15.8%" headers="mcps1.2.5.1.2 "><p id="p10631113915550"><a name="p10631113915550"></a><a name="p10631113915550"></a>string</p>
Z
zengyawen 已提交
301
    </td>
Z
zengyawen 已提交
302
    <td class="cellrowborder" valign="top" width="9.520000000000001%" headers="mcps1.2.5.1.3 "><p id="p36311239105519"><a name="p36311239105519"></a><a name="p36311239105519"></a>是</p>
Z
zengyawen 已提交
303
    </td>
Z
zengyawen 已提交
304
    <td class="cellrowborder" valign="top" width="58.220000000000006%" headers="mcps1.2.5.1.4 "><p id="p17767351572"><a name="p17767351572"></a><a name="p17767351572"></a>当前遍历到的键值。</p>
Z
zengyawen 已提交
305 306
    </td>
    </tr>
Z
zengyawen 已提交
307
    <tr id="row1463119397553"><td class="cellrowborder" valign="top" width="16.46%" headers="mcps1.2.5.1.1 "><p id="p19631103985517"><a name="p19631103985517"></a><a name="p19631103985517"></a>key</p>
Z
zengyawen 已提交
308
    </td>
Z
zengyawen 已提交
309
    <td class="cellrowborder" valign="top" width="15.8%" headers="mcps1.2.5.1.2 "><p id="p7631113965516"><a name="p7631113965516"></a><a name="p7631113965516"></a>string</p>
Z
zengyawen 已提交
310
    </td>
Z
zengyawen 已提交
311
    <td class="cellrowborder" valign="top" width="9.520000000000001%" headers="mcps1.2.5.1.3 "><p id="p2631203918552"><a name="p2631203918552"></a><a name="p2631203918552"></a>是</p>
Z
zengyawen 已提交
312
    </td>
Z
zengyawen 已提交
313
    <td class="cellrowborder" valign="top" width="58.220000000000006%" headers="mcps1.2.5.1.4 "><p id="p186310399556"><a name="p186310399556"></a><a name="p186310399556"></a>当前遍历到的键名。</p>
Z
zengyawen 已提交
314 315
    </td>
    </tr>
Z
zengyawen 已提交
316
    <tr id="row19539259115610"><td class="cellrowborder" valign="top" width="16.46%" headers="mcps1.2.5.1.1 "><p id="p980433316187"><a name="p980433316187"></a><a name="p980433316187"></a>searchParams</p>
Z
zengyawen 已提交
317
    </td>
Z
zengyawen 已提交
318
    <td class="cellrowborder" valign="top" width="15.8%" headers="mcps1.2.5.1.2 "><p id="p1453910594565"><a name="p1453910594565"></a><a name="p1453910594565"></a>Object</p>
Z
zengyawen 已提交
319
    </td>
Z
zengyawen 已提交
320
    <td class="cellrowborder" valign="top" width="9.520000000000001%" headers="mcps1.2.5.1.3 "><p id="p35412590563"><a name="p35412590563"></a><a name="p35412590563"></a>是</p>
Z
zengyawen 已提交
321
    </td>
Z
zengyawen 已提交
322
    <td class="cellrowborder" valign="top" width="58.220000000000006%" headers="mcps1.2.5.1.4 "><p id="p1323294318182"><a name="p1323294318182"></a><a name="p1323294318182"></a>当前调用forEach方法的实例对象。</p>
Z
zengyawen 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    </td>
    </tr>
    </tbody>
    </table>

-   示例:

    ```
    const myURLObject = new URL('https://developer.exampleUrl/?fod=1&bard=2'); 
    myURLObject.searchParams.forEach((value, name, searchParams) => {  
        console.log(name, value, myURLObject.searchParams === searchParams);
    });
    ```


Z
zengyawen 已提交
338
### get<a name="section1229318155373"></a>
Z
zengyawen 已提交
339 340 341 342 343 344 345

get\(name: string\): string | null

获取指定名称对应的第一个值。

-   参数:

Z
zengyawen 已提交
346 347
    <a name="table377217542586"></a>
    <table><thead align="left"><tr id="row1877275419580"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p277225412589"><a name="p277225412589"></a><a name="p277225412589"></a>参数名</p>
Z
zengyawen 已提交
348
    </th>
Z
zengyawen 已提交
349
    <th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p10772175495820"><a name="p10772175495820"></a><a name="p10772175495820"></a>类型</p>
Z
zengyawen 已提交
350
    </th>
Z
zengyawen 已提交
351
    <th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p277265412588"><a name="p277265412588"></a><a name="p277265412588"></a>必填</p>
Z
zengyawen 已提交
352
    </th>
Z
zengyawen 已提交
353
    <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p77731254105819"><a name="p77731254105819"></a><a name="p77731254105819"></a>说明</p>
Z
zengyawen 已提交
354 355 356
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
357
    <tbody><tr id="row4773854175820"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p137734549583"><a name="p137734549583"></a><a name="p137734549583"></a>name</p>
Z
zengyawen 已提交
358
    </td>
Z
zengyawen 已提交
359
    <td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p107730545586"><a name="p107730545586"></a><a name="p107730545586"></a>string</p>
Z
zengyawen 已提交
360
    </td>
Z
zengyawen 已提交
361
    <td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p277355418585"><a name="p277355418585"></a><a name="p277355418585"></a>是</p>
Z
zengyawen 已提交
362
    </td>
Z
zengyawen 已提交
363
    <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p877335412588"><a name="p877335412588"></a><a name="p877335412588"></a>指定键值对的名称。</p>
Z
zengyawen 已提交
364 365 366 367 368 369 370
    </td>
    </tr>
    </tbody>
    </table>

-   返回值:

Z
zengyawen 已提交
371 372
    <a name="table18354510245"></a>
    <table><thead align="left"><tr id="row133612552415"><th class="cellrowborder" valign="top" width="20.96%" id="mcps1.1.3.1.1"><p id="p8369592415"><a name="p8369592415"></a><a name="p8369592415"></a>类型</p>
Z
zengyawen 已提交
373
    </th>
Z
zengyawen 已提交
374
    <th class="cellrowborder" valign="top" width="79.03999999999999%" id="mcps1.1.3.1.2"><p id="p1363514249"><a name="p1363514249"></a><a name="p1363514249"></a>说明</p>
Z
zengyawen 已提交
375 376 377
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
378
    <tbody><tr id="row43614517248"><td class="cellrowborder" valign="top" width="20.96%" headers="mcps1.1.3.1.1 "><p id="p193611582417"><a name="p193611582417"></a><a name="p193611582417"></a>string</p>
Z
zengyawen 已提交
379
    </td>
Z
zengyawen 已提交
380
    <td class="cellrowborder" valign="top" width="79.03999999999999%" headers="mcps1.1.3.1.2 "><p id="p136452242"><a name="p136452242"></a><a name="p136452242"></a>返回第一个值。</p>
Z
zengyawen 已提交
381 382
    </td>
    </tr>
Z
zengyawen 已提交
383
    <tr id="row1190673810242"><td class="cellrowborder" valign="top" width="20.96%" headers="mcps1.1.3.1.1 "><p id="p11907163819242"><a name="p11907163819242"></a><a name="p11907163819242"></a>null</p>
Z
zengyawen 已提交
384
    </td>
Z
zengyawen 已提交
385
    <td class="cellrowborder" valign="top" width="79.03999999999999%" headers="mcps1.1.3.1.2 "><p id="p7907183802419"><a name="p7907183802419"></a><a name="p7907183802419"></a>如果没找到,返回 null。</p>
Z
zengyawen 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    </td>
    </tr>
    </tbody>
    </table>

-   示例:

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


Z
zengyawen 已提交
401
### has<a name="section1385211103813"></a>
Z
zengyawen 已提交
402 403 404 405 406 407 408

has\(name: string\): boolean

判断一个指定的键名对应的值是否存在。

-   参数:

Z
zengyawen 已提交
409 410
    <a name="table746711171833"></a>
    <table><thead align="left"><tr id="row174684171316"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p154681517736"><a name="p154681517736"></a><a name="p154681517736"></a>参数名</p>
Z
zengyawen 已提交
411
    </th>
Z
zengyawen 已提交
412
    <th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p24684172312"><a name="p24684172312"></a><a name="p24684172312"></a>类型</p>
Z
zengyawen 已提交
413
    </th>
Z
zengyawen 已提交
414
    <th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p94681817736"><a name="p94681817736"></a><a name="p94681817736"></a>必填</p>
Z
zengyawen 已提交
415
    </th>
Z
zengyawen 已提交
416
    <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p646817171331"><a name="p646817171331"></a><a name="p646817171331"></a>说明</p>
Z
zengyawen 已提交
417 418 419
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
420
    <tbody><tr id="row134681171135"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p64692017333"><a name="p64692017333"></a><a name="p64692017333"></a>name</p>
Z
zengyawen 已提交
421
    </td>
Z
zengyawen 已提交
422
    <td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p1946910174320"><a name="p1946910174320"></a><a name="p1946910174320"></a>string</p>
Z
zengyawen 已提交
423
    </td>
Z
zengyawen 已提交
424
    <td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p10469717231"><a name="p10469717231"></a><a name="p10469717231"></a>是</p>
Z
zengyawen 已提交
425
    </td>
Z
zengyawen 已提交
426
    <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p24691217436"><a name="p24691217436"></a><a name="p24691217436"></a>要查找的参数的键名。</p>
Z
zengyawen 已提交
427 428 429 430 431 432 433
    </td>
    </tr>
    </tbody>
    </table>

-   返回值:

Z
zengyawen 已提交
434 435
    <a name="table156691044124411"></a>
    <table><thead align="left"><tr id="row6670164444419"><th class="cellrowborder" valign="top" width="20.990000000000002%" id="mcps1.1.3.1.1"><p id="p10670114414449"><a name="p10670114414449"></a><a name="p10670114414449"></a>类型</p>
Z
zengyawen 已提交
436
    </th>
Z
zengyawen 已提交
437
    <th class="cellrowborder" valign="top" width="79.01%" id="mcps1.1.3.1.2"><p id="p3670144424418"><a name="p3670144424418"></a><a name="p3670144424418"></a>说明</p>
Z
zengyawen 已提交
438 439 440
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
441
    <tbody><tr id="row13670184418448"><td class="cellrowborder" valign="top" width="20.990000000000002%" headers="mcps1.1.3.1.1 "><p id="p1667064474420"><a name="p1667064474420"></a><a name="p1667064474420"></a>boolean</p>
Z
zengyawen 已提交
442
    </td>
Z
zengyawen 已提交
443
    <td class="cellrowborder" valign="top" width="79.01%" headers="mcps1.1.3.1.2 "><p id="p5592172072510"><a name="p5592172072510"></a><a name="p5592172072510"></a>是否存在相对应的key值,存在返回true,否则返回false。</p>
Z
zengyawen 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457
    </td>
    </tr>
    </tbody>
    </table>

-   示例:

    ```
    let urlObject = new URL('https://developer.exampleUrl/?fod=1&bard=2');
    let paramsObject = new URLSearchParams(urlObject.search.slice(1)); 
    paramsObject.has('bard') === true;
    ```


Z
zengyawen 已提交
458
### set<a name="section186091516203910"></a>
Z
zengyawen 已提交
459 460 461 462 463 464 465

set\(name: string, value: string\): void

将与name关联的URLSearchParams对象中的值设置为value。如果存在名称为name的键值对,请将第一个键值对的值设置为value并删除所有其他值。如果不是,则将键值对附加到查询字符串。

-   参数:

Z
zengyawen 已提交
466 467
    <a name="table1653110211471"></a>
    <table><thead align="left"><tr id="row7531721778"><th class="cellrowborder" valign="top" width="14.82%" id="mcps1.1.5.1.1"><p id="p75312021371"><a name="p75312021371"></a><a name="p75312021371"></a>参数名</p>
Z
zengyawen 已提交
468
    </th>
Z
zengyawen 已提交
469
    <th class="cellrowborder" valign="top" width="14.729999999999999%" id="mcps1.1.5.1.2"><p id="p105313219716"><a name="p105313219716"></a><a name="p105313219716"></a>类型</p>
Z
zengyawen 已提交
470
    </th>
Z
zengyawen 已提交
471
    <th class="cellrowborder" valign="top" width="9.16%" id="mcps1.1.5.1.3"><p id="p1553216216717"><a name="p1553216216717"></a><a name="p1553216216717"></a>必填</p>
Z
zengyawen 已提交
472
    </th>
Z
zengyawen 已提交
473
    <th class="cellrowborder" valign="top" width="61.29%" id="mcps1.1.5.1.4"><p id="p13532102114716"><a name="p13532102114716"></a><a name="p13532102114716"></a>说明</p>
Z
zengyawen 已提交
474 475 476
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
477
    <tbody><tr id="row185321921572"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p10532172118711"><a name="p10532172118711"></a><a name="p10532172118711"></a>name</p>
Z
zengyawen 已提交
478
    </td>
Z
zengyawen 已提交
479
    <td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p10532152114718"><a name="p10532152114718"></a><a name="p10532152114718"></a>string</p>
Z
zengyawen 已提交
480
    </td>
Z
zengyawen 已提交
481
    <td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p1453220215713"><a name="p1453220215713"></a><a name="p1453220215713"></a>是</p>
Z
zengyawen 已提交
482
    </td>
Z
zengyawen 已提交
483
    <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p053215214717"><a name="p053215214717"></a><a name="p053215214717"></a>将要设置的参数的键值名。</p>
Z
zengyawen 已提交
484 485
    </td>
    </tr>
Z
zengyawen 已提交
486
    <tr id="row18550750182"><td class="cellrowborder" valign="top" width="14.82%" headers="mcps1.1.5.1.1 "><p id="p655013501184"><a name="p655013501184"></a><a name="p655013501184"></a>value</p>
Z
zengyawen 已提交
487
    </td>
Z
zengyawen 已提交
488
    <td class="cellrowborder" valign="top" width="14.729999999999999%" headers="mcps1.1.5.1.2 "><p id="p196231541790"><a name="p196231541790"></a><a name="p196231541790"></a>string</p>
Z
zengyawen 已提交
489
    </td>
Z
zengyawen 已提交
490
    <td class="cellrowborder" valign="top" width="9.16%" headers="mcps1.1.5.1.3 "><p id="p355120502810"><a name="p355120502810"></a><a name="p355120502810"></a>是</p>
Z
zengyawen 已提交
491
    </td>
Z
zengyawen 已提交
492
    <td class="cellrowborder" valign="top" width="61.29%" headers="mcps1.1.5.1.4 "><p id="p145514507816"><a name="p145514507816"></a><a name="p145514507816"></a>所要设置的参数值。</p>
Z
zengyawen 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

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


Z
zengyawen 已提交
508
### sort<a name="section102851552183915"></a>
Z
zengyawen 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522

sort\(\): void

对包含在此对象中的所有键值对进行排序,并返回undefined。排序顺序是根据键的Unicode代码点。该方法使用稳定的排序算法 (即,将保留具有相等键的键值对之间的相对顺序)。

-   示例:

    ```
    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 已提交
523
### keys<a name="section816918183413"></a>
Z
zengyawen 已提交
524 525 526 527 528 529 530

keys\(\): IterableIterator<string\>

返回一个所有键值对的name的ES6迭代器。

-   返回值:

Z
zengyawen 已提交
531 532
    <a name="table15822151784820"></a>
    <table><thead align="left"><tr id="row38222172484"><th class="cellrowborder" valign="top" width="32.550000000000004%" id="mcps1.1.3.1.1"><p id="p158225175483"><a name="p158225175483"></a><a name="p158225175483"></a>类型</p>
Z
zengyawen 已提交
533
    </th>
Z
zengyawen 已提交
534
    <th class="cellrowborder" valign="top" width="67.45%" id="mcps1.1.3.1.2"><p id="p3822131718485"><a name="p3822131718485"></a><a name="p3822131718485"></a>说明</p>
Z
zengyawen 已提交
535 536 537
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
538
    <tbody><tr id="row58231817124817"><td class="cellrowborder" valign="top" width="32.550000000000004%" headers="mcps1.1.3.1.1 "><p id="p118232017164813"><a name="p118232017164813"></a><a name="p118232017164813"></a>IterableIterator&lt;string&gt;</p>
Z
zengyawen 已提交
539
    </td>
Z
zengyawen 已提交
540
    <td class="cellrowborder" valign="top" width="67.45%" headers="mcps1.1.3.1.2 "><p id="p1128142484911"><a name="p1128142484911"></a><a name="p1128142484911"></a>返回一个所有键值对的name的ES6迭代器。</p>
Z
zengyawen 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

    ```
    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);
    }
    ```


Z
zengyawen 已提交
557
### values<a name="section0542174314413"></a>
Z
zengyawen 已提交
558 559 560 561 562 563 564

values\(\): IterableIterator<string\>

返回一个所有键值对的value的ES6迭代器。

-   返回值:

Z
zengyawen 已提交
565 566
    <a name="table44901259134916"></a>
    <table><thead align="left"><tr id="row44901859124916"><th class="cellrowborder" valign="top" width="36.51%" id="mcps1.1.3.1.1"><p id="p19491175912499"><a name="p19491175912499"></a><a name="p19491175912499"></a>类型</p>
Z
zengyawen 已提交
567
    </th>
Z
zengyawen 已提交
568
    <th class="cellrowborder" valign="top" width="63.49%" id="mcps1.1.3.1.2"><p id="p1491145934914"><a name="p1491145934914"></a><a name="p1491145934914"></a>说明</p>
Z
zengyawen 已提交
569 570 571
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
572
    <tbody><tr id="row6491125914919"><td class="cellrowborder" valign="top" width="36.51%" headers="mcps1.1.3.1.1 "><p id="p13812011488"><a name="p13812011488"></a><a name="p13812011488"></a>IterableIterator&lt;string&gt;</p>
Z
zengyawen 已提交
573
    </td>
Z
zengyawen 已提交
574
    <td class="cellrowborder" valign="top" width="63.49%" headers="mcps1.1.3.1.2 "><p id="p38375645210"><a name="p38375645210"></a><a name="p38375645210"></a>返回一个所有键值对的value的ES6迭代器。</p>
Z
zengyawen 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    </td>
    </tr>
    </tbody>
    </table>


-   示例

    ```
    var searchParams = new URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing
    for (var value of searchParams.values()) { 
        console.log(value);
    }
    ```


Z
zengyawen 已提交
591
### \[Symbol.iterator\]<a name="section1152151784210"></a>
Z
zengyawen 已提交
592 593 594 595 596 597 598

\[Symbol.iterator\]\(\): IterableIterator<\[string, string\]\>

返回一个ES6的迭代器,迭代器的每一项都是一个 JavaScript Array。Array的第一项是name,Array的第二项是value。

-   返回值:

Z
zengyawen 已提交
599 600
    <a name="table8247125417537"></a>
    <table><thead align="left"><tr id="row1424715435311"><th class="cellrowborder" valign="top" width="38.99%" id="mcps1.1.3.1.1"><p id="p17247125419532"><a name="p17247125419532"></a><a name="p17247125419532"></a>类型</p>
Z
zengyawen 已提交
601
    </th>
Z
zengyawen 已提交
602
    <th class="cellrowborder" valign="top" width="61.01%" id="mcps1.1.3.1.2"><p id="p424795415313"><a name="p424795415313"></a><a name="p424795415313"></a>说明</p>
Z
zengyawen 已提交
603 604 605
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
606
    <tbody><tr id="row72471054115317"><td class="cellrowborder" valign="top" width="38.99%" headers="mcps1.1.3.1.1 "><p id="p8576493492"><a name="p8576493492"></a><a name="p8576493492"></a>IterableIterator&lt;[string, string]&gt;</p>
Z
zengyawen 已提交
607
    </td>
Z
zengyawen 已提交
608
    <td class="cellrowborder" valign="top" width="61.01%" headers="mcps1.1.3.1.2 "><p id="p16248185414532"><a name="p16248185414532"></a><a name="p16248185414532"></a>返回一个ES6的迭代器。</p>
Z
zengyawen 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

    ```
    const paramsObject = new URLSearchParams('fod=bay&edg=bap');
    for (const [name, value] of paramsObject) { 
        console.log(name, value); 
    } 
    ```


Z
zengyawen 已提交
625
### tostring<a name="section228585911428"></a>
Z
zengyawen 已提交
626 627 628 629 630 631 632

toString\(\): string

返回序列化为字符串的搜索参数,必要时对字符进行百分比编码。

-   返回值:

Z
zengyawen 已提交
633 634
    <a name="table016316018566"></a>
    <table><thead align="left"><tr id="row171632010566"><th class="cellrowborder" valign="top" width="20.990000000000002%" id="mcps1.1.3.1.1"><p id="p71631308563"><a name="p71631308563"></a><a name="p71631308563"></a>类型</p>
Z
zengyawen 已提交
635
    </th>
Z
zengyawen 已提交
636
    <th class="cellrowborder" valign="top" width="79.01%" id="mcps1.1.3.1.2"><p id="p2164120205610"><a name="p2164120205610"></a><a name="p2164120205610"></a>说明</p>
Z
zengyawen 已提交
637 638 639
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
640
    <tbody><tr id="row14164110145610"><td class="cellrowborder" valign="top" width="20.990000000000002%" headers="mcps1.1.3.1.1 "><p id="p1516413016565"><a name="p1516413016565"></a><a name="p1516413016565"></a>string</p>
Z
zengyawen 已提交
641
    </td>
Z
zengyawen 已提交
642
    <td class="cellrowborder" valign="top" width="79.01%" headers="mcps1.1.3.1.2 "><p id="p516412017563"><a name="p516412017563"></a><a name="p516412017563"></a>返回序列化为字符串的搜索参数,必要时对字符进行百分比编码。</p>
Z
zengyawen 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

    ```
    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());
    ```


Z
zengyawen 已提交
659
## URL<a name="section148871646203714"></a>
Z
zengyawen 已提交
660

Z
zengyawen 已提交
661
### 属性<a name="section18482944113517"></a>
Z
zengyawen 已提交
662

Z
zengyawen 已提交
663 664
<a name="table20482104463514"></a>
<table><thead align="left"><tr id="row1348214444357"><th class="cellrowborder" valign="top" width="11.84%" id="mcps1.1.6.1.1"><p id="p184822443351"><a name="p184822443351"></a><a name="p184822443351"></a>名称</p>
Z
zengyawen 已提交
665
</th>
Z
zengyawen 已提交
666
<th class="cellrowborder" valign="top" width="20.75%" id="mcps1.1.6.1.2"><p id="p1648254415353"><a name="p1648254415353"></a><a name="p1648254415353"></a>参数类型</p>
Z
zengyawen 已提交
667
</th>
Z
zengyawen 已提交
668
<th class="cellrowborder" valign="top" width="5.34%" id="mcps1.1.6.1.3"><p id="p9482124413355"><a name="p9482124413355"></a><a name="p9482124413355"></a>可读</p>
Z
zengyawen 已提交
669
</th>
Z
zengyawen 已提交
670
<th class="cellrowborder" valign="top" width="9.65%" id="mcps1.1.6.1.4"><p id="p3482124493515"><a name="p3482124493515"></a><a name="p3482124493515"></a>可写</p>
Z
zengyawen 已提交
671
</th>
Z
zengyawen 已提交
672
<th class="cellrowborder" valign="top" width="52.42%" id="mcps1.1.6.1.5"><p id="p12482104483513"><a name="p12482104483513"></a><a name="p12482104483513"></a>说明</p>
Z
zengyawen 已提交
673 674 675
</th>
</tr>
</thead>
Z
zengyawen 已提交
676
<tbody><tr id="row148254473513"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p1832319183511"><a name="p1832319183511"></a><a name="p1832319183511"></a>hash</p>
Z
zengyawen 已提交
677
</td>
Z
zengyawen 已提交
678
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p3482124417356"><a name="p3482124417356"></a><a name="p3482124417356"></a>string</p>
Z
zengyawen 已提交
679
</td>
Z
zengyawen 已提交
680
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p16483194443514"><a name="p16483194443514"></a><a name="p16483194443514"></a></p>
Z
zengyawen 已提交
681
</td>
Z
zengyawen 已提交
682
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p630714163422"><a name="p630714163422"></a><a name="p630714163422"></a></p>
Z
zengyawen 已提交
683
</td>
Z
zengyawen 已提交
684
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p72131224114411"><a name="p72131224114411"></a><a name="p72131224114411"></a>获取和设置URL的片段部分。</p>
Z
zengyawen 已提交
685 686
</td>
</tr>
Z
zengyawen 已提交
687
<tr id="row58631729183511"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p1669917383355"><a name="p1669917383355"></a><a name="p1669917383355"></a>host</p>
Z
zengyawen 已提交
688
</td>
Z
zengyawen 已提交
689
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p986510296359"><a name="p986510296359"></a><a name="p986510296359"></a>string</p>
Z
zengyawen 已提交
690
</td>
Z
zengyawen 已提交
691
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p14865182993518"><a name="p14865182993518"></a><a name="p14865182993518"></a></p>
Z
zengyawen 已提交
692
</td>
Z
zengyawen 已提交
693
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p1586552915355"><a name="p1586552915355"></a><a name="p1586552915355"></a></p>
Z
zengyawen 已提交
694
</td>
Z
zengyawen 已提交
695
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p12865829133514"><a name="p12865829133514"></a><a name="p12865829133514"></a>获取和设置URL的主机部分。</p>
Z
zengyawen 已提交
696 697
</td>
</tr>
Z
zengyawen 已提交
698
<tr id="row173737374371"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p73744376373"><a name="p73744376373"></a><a name="p73744376373"></a>hostname</p>
Z
zengyawen 已提交
699
</td>
Z
zengyawen 已提交
700
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p183741337173714"><a name="p183741337173714"></a><a name="p183741337173714"></a>string</p>
Z
zengyawen 已提交
701
</td>
Z
zengyawen 已提交
702
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p13374237173711"><a name="p13374237173711"></a><a name="p13374237173711"></a></p>
Z
zengyawen 已提交
703
</td>
Z
zengyawen 已提交
704
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p1737416372376"><a name="p1737416372376"></a><a name="p1737416372376"></a></p>
Z
zengyawen 已提交
705
</td>
Z
zengyawen 已提交
706
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p737414373372"><a name="p737414373372"></a><a name="p737414373372"></a>获取和设置URL的主机名部分,不带端口。</p>
Z
zengyawen 已提交
707 708
</td>
</tr>
Z
zengyawen 已提交
709
<tr id="row18603545143715"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p1360410454373"><a name="p1360410454373"></a><a name="p1360410454373"></a>href</p>
Z
zengyawen 已提交
710
</td>
Z
zengyawen 已提交
711
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p1260410456378"><a name="p1260410456378"></a><a name="p1260410456378"></a>string</p>
Z
zengyawen 已提交
712
</td>
Z
zengyawen 已提交
713
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p146042453371"><a name="p146042453371"></a><a name="p146042453371"></a></p>
Z
zengyawen 已提交
714
</td>
Z
zengyawen 已提交
715
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p460416456374"><a name="p460416456374"></a><a name="p460416456374"></a></p>
Z
zengyawen 已提交
716
</td>
Z
zengyawen 已提交
717
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p1560554514376"><a name="p1560554514376"></a><a name="p1560554514376"></a>获取和设置序列化的URL。</p>
Z
zengyawen 已提交
718 719
</td>
</tr>
Z
zengyawen 已提交
720
<tr id="row15118124793712"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p9121914204110"><a name="p9121914204110"></a><a name="p9121914204110"></a>origin</p>
Z
zengyawen 已提交
721
</td>
Z
zengyawen 已提交
722
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p41181471376"><a name="p41181471376"></a><a name="p41181471376"></a>string</p>
Z
zengyawen 已提交
723
</td>
Z
zengyawen 已提交
724
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p10118447153719"><a name="p10118447153719"></a><a name="p10118447153719"></a></p>
Z
zengyawen 已提交
725
</td>
Z
zengyawen 已提交
726
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p1111884793714"><a name="p1111884793714"></a><a name="p1111884793714"></a></p>
Z
zengyawen 已提交
727
</td>
Z
zengyawen 已提交
728
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p11181479377"><a name="p11181479377"></a><a name="p11181479377"></a>获取URL源的只读序列化。</p>
Z
zengyawen 已提交
729 730
</td>
</tr>
Z
zengyawen 已提交
731
<tr id="row2998164312378"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p20998643103713"><a name="p20998643103713"></a><a name="p20998643103713"></a>password</p>
Z
zengyawen 已提交
732
</td>
Z
zengyawen 已提交
733
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p1899804315372"><a name="p1899804315372"></a><a name="p1899804315372"></a>string</p>
Z
zengyawen 已提交
734
</td>
Z
zengyawen 已提交
735
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p1399820431377"><a name="p1399820431377"></a><a name="p1399820431377"></a></p>
Z
zengyawen 已提交
736
</td>
Z
zengyawen 已提交
737
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p29980434371"><a name="p29980434371"></a><a name="p29980434371"></a></p>
Z
zengyawen 已提交
738
</td>
Z
zengyawen 已提交
739
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p1299814373714"><a name="p1299814373714"></a><a name="p1299814373714"></a>获取和设置URL的密码部分。</p>
Z
zengyawen 已提交
740 741
</td>
</tr>
Z
zengyawen 已提交
742
<tr id="row6527542193711"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p1852784212370"><a name="p1852784212370"></a><a name="p1852784212370"></a>pathname</p>
Z
zengyawen 已提交
743
</td>
Z
zengyawen 已提交
744
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p205271742183711"><a name="p205271742183711"></a><a name="p205271742183711"></a>string</p>
Z
zengyawen 已提交
745
</td>
Z
zengyawen 已提交
746
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p952724233717"><a name="p952724233717"></a><a name="p952724233717"></a></p>
Z
zengyawen 已提交
747
</td>
Z
zengyawen 已提交
748
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p1452784214373"><a name="p1452784214373"></a><a name="p1452784214373"></a></p>
Z
zengyawen 已提交
749
</td>
Z
zengyawen 已提交
750
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p18527842143718"><a name="p18527842143718"></a><a name="p18527842143718"></a>获取和设置URL的路径部分。</p>
Z
zengyawen 已提交
751 752
</td>
</tr>
Z
zengyawen 已提交
753
<tr id="row1221604019378"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p12161040193711"><a name="p12161040193711"></a><a name="p12161040193711"></a>port</p>
Z
zengyawen 已提交
754
</td>
Z
zengyawen 已提交
755
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p12161040103714"><a name="p12161040103714"></a><a name="p12161040103714"></a>string</p>
Z
zengyawen 已提交
756
</td>
Z
zengyawen 已提交
757
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p621674073715"><a name="p621674073715"></a><a name="p621674073715"></a></p>
Z
zengyawen 已提交
758
</td>
Z
zengyawen 已提交
759
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p1821624033711"><a name="p1821624033711"></a><a name="p1821624033711"></a></p>
Z
zengyawen 已提交
760
</td>
Z
zengyawen 已提交
761
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p1421614403377"><a name="p1421614403377"></a><a name="p1421614403377"></a>获取和设置URL的端口部分。</p>
Z
zengyawen 已提交
762 763
</td>
</tr>
Z
zengyawen 已提交
764
<tr id="row763211357376"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p1863313520378"><a name="p1863313520378"></a><a name="p1863313520378"></a>protocol</p>
Z
zengyawen 已提交
765
</td>
Z
zengyawen 已提交
766
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p8633113583715"><a name="p8633113583715"></a><a name="p8633113583715"></a>string</p>
Z
zengyawen 已提交
767
</td>
Z
zengyawen 已提交
768
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p1663363533710"><a name="p1663363533710"></a><a name="p1663363533710"></a></p>
Z
zengyawen 已提交
769
</td>
Z
zengyawen 已提交
770
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p1763312358374"><a name="p1763312358374"></a><a name="p1763312358374"></a></p>
Z
zengyawen 已提交
771
</td>
Z
zengyawen 已提交
772
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p363383518374"><a name="p363383518374"></a><a name="p363383518374"></a>获取和设置URL的协议部分。</p>
Z
zengyawen 已提交
773 774
</td>
</tr>
Z
zengyawen 已提交
775
<tr id="row252344113516"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p852414113510"><a name="p852414113510"></a><a name="p852414113510"></a>search</p>
Z
zengyawen 已提交
776
</td>
Z
zengyawen 已提交
777
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p115241141203517"><a name="p115241141203517"></a><a name="p115241141203517"></a>string</p>
Z
zengyawen 已提交
778
</td>
Z
zengyawen 已提交
779
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p3524134183519"><a name="p3524134183519"></a><a name="p3524134183519"></a></p>
Z
zengyawen 已提交
780
</td>
Z
zengyawen 已提交
781
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p752464173511"><a name="p752464173511"></a><a name="p752464173511"></a></p>
Z
zengyawen 已提交
782
</td>
Z
zengyawen 已提交
783
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p12524144111358"><a name="p12524144111358"></a><a name="p12524144111358"></a>获取和设置URL的序列化查询部分。</p>
Z
zengyawen 已提交
784 785
</td>
</tr>
Z
zengyawen 已提交
786
<tr id="row1483164414352"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p0747823153511"><a name="p0747823153511"></a><a name="p0747823153511"></a>searchParams</p>
Z
zengyawen 已提交
787
</td>
Z
zengyawen 已提交
788
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p133371031194316"><a name="p133371031194316"></a><a name="p133371031194316"></a>URLsearchParams</p>
Z
zengyawen 已提交
789
</td>
Z
zengyawen 已提交
790
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p1348384415359"><a name="p1348384415359"></a><a name="p1348384415359"></a></p>
Z
zengyawen 已提交
791
</td>
Z
zengyawen 已提交
792
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p1448312449355"><a name="p1448312449355"></a><a name="p1448312449355"></a></p>
Z
zengyawen 已提交
793
</td>
Z
zengyawen 已提交
794
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p8483124473515"><a name="p8483124473515"></a><a name="p8483124473515"></a>获取URLSearchParams表示URL查询参数的对象。</p>
Z
zengyawen 已提交
795 796
</td>
</tr>
Z
zengyawen 已提交
797
<tr id="row13483164419353"><td class="cellrowborder" valign="top" width="11.84%" headers="mcps1.1.6.1.1 "><p id="p4483154463514"><a name="p4483154463514"></a><a name="p4483154463514"></a>username</p>
Z
zengyawen 已提交
798
</td>
Z
zengyawen 已提交
799
<td class="cellrowborder" valign="top" width="20.75%" headers="mcps1.1.6.1.2 "><p id="p1887911084315"><a name="p1887911084315"></a><a name="p1887911084315"></a>string</p>
Z
zengyawen 已提交
800
</td>
Z
zengyawen 已提交
801
<td class="cellrowborder" valign="top" width="5.34%" headers="mcps1.1.6.1.3 "><p id="p1483134419353"><a name="p1483134419353"></a><a name="p1483134419353"></a></p>
Z
zengyawen 已提交
802
</td>
Z
zengyawen 已提交
803
<td class="cellrowborder" valign="top" width="9.65%" headers="mcps1.1.6.1.4 "><p id="p648354483510"><a name="p648354483510"></a><a name="p648354483510"></a></p>
Z
zengyawen 已提交
804
</td>
Z
zengyawen 已提交
805
<td class="cellrowborder" valign="top" width="52.42%" headers="mcps1.1.6.1.5 "><p id="p348319442351"><a name="p348319442351"></a><a name="p348319442351"></a>获取和设置URL的用户名部分。</p>
Z
zengyawen 已提交
806 807 808 809 810
</td>
</tr>
</tbody>
</table>

Z
zengyawen 已提交
811
### constructor<a name="section712011584410"></a>
Z
zengyawen 已提交
812 813 814 815 816 817 818

constructor\(url: string, base?: string | URL\)

URL的构造函数。

-   参数:

Z
zengyawen 已提交
819 820
    <a name="table1723015271384"></a>
    <table><thead align="left"><tr id="row42301327153814"><th class="cellrowborder" valign="top" width="17.5%" id="mcps1.1.5.1.1"><p id="p102307272383"><a name="p102307272383"></a><a name="p102307272383"></a>参数名</p>
Z
zengyawen 已提交
821
    </th>
Z
zengyawen 已提交
822
    <th class="cellrowborder" valign="top" width="19.139999999999997%" id="mcps1.1.5.1.2"><p id="p202307278382"><a name="p202307278382"></a><a name="p202307278382"></a>类型</p>
Z
zengyawen 已提交
823
    </th>
Z
zengyawen 已提交
824
    <th class="cellrowborder" valign="top" width="13.81%" id="mcps1.1.5.1.3"><p id="p1123113278381"><a name="p1123113278381"></a><a name="p1123113278381"></a>必填</p>
Z
zengyawen 已提交
825
    </th>
Z
zengyawen 已提交
826
    <th class="cellrowborder" valign="top" width="49.55%" id="mcps1.1.5.1.4"><p id="p5231172773815"><a name="p5231172773815"></a><a name="p5231172773815"></a>说明</p>
Z
zengyawen 已提交
827 828 829
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
830
    <tbody><tr id="row122318279387"><td class="cellrowborder" valign="top" width="17.5%" headers="mcps1.1.5.1.1 "><p id="p161271253807"><a name="p161271253807"></a><a name="p161271253807"></a>url</p>
Z
zengyawen 已提交
831
    </td>
Z
zengyawen 已提交
832
    <td class="cellrowborder" valign="top" width="19.139999999999997%" headers="mcps1.1.5.1.2 "><p id="p11231102719382"><a name="p11231102719382"></a><a name="p11231102719382"></a>string</p>
Z
zengyawen 已提交
833
    </td>
Z
zengyawen 已提交
834
    <td class="cellrowborder" valign="top" width="13.81%" headers="mcps1.1.5.1.3 "><p id="p1023152712389"><a name="p1023152712389"></a><a name="p1023152712389"></a>是</p>
Z
zengyawen 已提交
835
    </td>
Z
zengyawen 已提交
836
    <td class="cellrowborder" valign="top" width="49.55%" headers="mcps1.1.5.1.4 "><p id="p323132783819"><a name="p323132783819"></a><a name="p323132783819"></a>入参对象。</p>
Z
zengyawen 已提交
837 838
    </td>
    </tr>
Z
zengyawen 已提交
839
    <tr id="row189010267539"><td class="cellrowborder" valign="top" width="17.5%" headers="mcps1.1.5.1.1 "><p id="p1990210261536"><a name="p1990210261536"></a><a name="p1990210261536"></a>base</p>
Z
zengyawen 已提交
840
    </td>
Z
zengyawen 已提交
841
    <td class="cellrowborder" valign="top" width="19.139999999999997%" headers="mcps1.1.5.1.2 "><p id="p159024261532"><a name="p159024261532"></a><a name="p159024261532"></a>string | URL</p>
Z
zengyawen 已提交
842
    </td>
Z
zengyawen 已提交
843
    <td class="cellrowborder" valign="top" width="13.81%" headers="mcps1.1.5.1.3 "><p id="p10902426145317"><a name="p10902426145317"></a><a name="p10902426145317"></a>否</p>
Z
zengyawen 已提交
844
    </td>
Z
zengyawen 已提交
845 846
    <td class="cellrowborder" valign="top" width="49.55%" headers="mcps1.1.5.1.4 "><p id="p16902126135313"><a name="p16902126135313"></a><a name="p16902126135313"></a>入参字符串或者对象。</p>
    <a name="ul1755124022517"></a><a name="ul1755124022517"></a><ul id="ul1755124022517"><li>string:字符串</li><li>URL:字符串或对象</li></ul>
Z
zengyawen 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

    ```
    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 已提交
871
### tostring<a name="section2849448104520"></a>
Z
zengyawen 已提交
872 873 874 875 876 877 878

toString\(\): string

将解析过后的URL转化为字符串。

-   返回值:

Z
zengyawen 已提交
879 880
    <a name="table201041412244"></a>
    <table><thead align="left"><tr id="row410431122411"><th class="cellrowborder" valign="top" width="20.990000000000002%" id="mcps1.1.3.1.1"><p id="p110413182418"><a name="p110413182418"></a><a name="p110413182418"></a>类型</p>
Z
zengyawen 已提交
881
    </th>
Z
zengyawen 已提交
882
    <th class="cellrowborder" valign="top" width="79.01%" id="mcps1.1.3.1.2"><p id="p181042116249"><a name="p181042116249"></a><a name="p181042116249"></a>说明</p>
Z
zengyawen 已提交
883 884 885
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
886
    <tbody><tr id="row110421182413"><td class="cellrowborder" valign="top" width="20.990000000000002%" headers="mcps1.1.3.1.1 "><p id="p510461102411"><a name="p510461102411"></a><a name="p510461102411"></a>string</p>
Z
zengyawen 已提交
887
    </td>
Z
zengyawen 已提交
888
    <td class="cellrowborder" valign="top" width="79.01%" headers="mcps1.1.3.1.2 "><p id="p159992542327"><a name="p159992542327"></a><a name="p159992542327"></a>用于返回网址的字符串序列化。</p>
Z
zengyawen 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901 902
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

    ```
    const url = new URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
    url.toString()
    ```


Z
zengyawen 已提交
903
### toJSON<a name="section3488111554611"></a>
Z
zengyawen 已提交
904 905 906 907 908 909 910

toJSON\(\): string

将解析过后的URL转化为JSON字符串。

-   返回值:

Z
zengyawen 已提交
911 912
    <a name="table981932613263"></a>
    <table><thead align="left"><tr id="row38194264264"><th class="cellrowborder" valign="top" width="20.990000000000002%" id="mcps1.1.3.1.1"><p id="p178208263269"><a name="p178208263269"></a><a name="p178208263269"></a>类型</p>
Z
zengyawen 已提交
913
    </th>
Z
zengyawen 已提交
914
    <th class="cellrowborder" valign="top" width="79.01%" id="mcps1.1.3.1.2"><p id="p15820112672617"><a name="p15820112672617"></a><a name="p15820112672617"></a>说明</p>
Z
zengyawen 已提交
915 916 917
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
918
    <tbody><tr id="row2820172622610"><td class="cellrowborder" valign="top" width="20.990000000000002%" headers="mcps1.1.3.1.1 "><p id="p13820122613264"><a name="p13820122613264"></a><a name="p13820122613264"></a>string</p>
Z
zengyawen 已提交
919
    </td>
Z
zengyawen 已提交
920
    <td class="cellrowborder" valign="top" width="79.01%" headers="mcps1.1.3.1.2 "><p id="p18497141212339"><a name="p18497141212339"></a><a name="p18497141212339"></a>用于返回网址的字符串序列化。</p>
Z
zengyawen 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934
    </td>
    </tr>
    </tbody>
    </table>


-   示例:

    ```
    const url = new URL('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
    url.toString()
    ```