@@ -92,6 +92,7 @@ Creates a new `URL` object by parsing the `input` relative to the `base`. If
92
92
` base ` is passed as a string, it will be parsed equivalent to ` new URL(base) ` .
93
93
94
94
``` js
95
+ const { URL } = require (' url' );
95
96
const myURL = new URL (' /foo' , ' https://example.org/' );
96
97
// https://example.org/foo
97
98
```
@@ -101,6 +102,7 @@ that an effort will be made to coerce the given values into strings. For
101
102
instance:
102
103
103
104
``` js
105
+ const { URL } = require (' url' );
104
106
const myURL = new URL ({ toString : () => ' https://example.org/' });
105
107
// https://example.org/
106
108
```
@@ -109,6 +111,7 @@ Unicode characters appearing within the hostname of `input` will be
109
111
automatically converted to ASCII using the [ Punycode] [ ] algorithm.
110
112
111
113
``` js
114
+ const { URL } = require (' url' );
112
115
const myURL = new URL (' https://你好你好' );
113
116
// https://xn--6qqa088eba/
114
117
```
@@ -122,6 +125,7 @@ Additional [examples of parsed URLs][] may be found in the WHATWG URL Standard.
122
125
Gets and sets the fragment portion of the URL.
123
126
124
127
``` js
128
+ const { URL } = require (' url' );
125
129
const myURL = new URL (' https://example.org/foo#bar' );
126
130
console .log (myURL .hash );
127
131
// Prints #bar
@@ -143,6 +147,7 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
143
147
Gets and sets the host portion of the URL.
144
148
145
149
``` js
150
+ const { URL } = require (' url' );
146
151
const myURL = new URL (' https://example.org:81/foo' );
147
152
console .log (myURL .host );
148
153
// Prints example.org:81
@@ -163,6 +168,7 @@ Gets and sets the hostname portion of the URL. The key difference between
163
168
port.
164
169
165
170
``` js
171
+ const { URL } = require (' url' );
166
172
const myURL = new URL (' https://example.org:81/foo' );
167
173
console .log (myURL .hostname );
168
174
// Prints example.org
@@ -181,6 +187,7 @@ Invalid hostname values assigned to the `hostname` property are ignored.
181
187
Gets and sets the serialized URL.
182
188
183
189
``` js
190
+ const { URL } = require (' url' );
184
191
const myURL = new URL (' https://example.org/foo' );
185
192
console .log (myURL .href );
186
193
// Prints https://example.org/foo
@@ -209,12 +216,14 @@ may be contained within the hostname will be encoded as-is without [Punycode][]
209
216
encoding.
210
217
211
218
``` js
219
+ const { URL } = require (' url' );
212
220
const myURL = new URL (' https://example.org/foo/bar?baz' );
213
221
console .log (myURL .origin );
214
222
// Prints https://example.org
215
223
```
216
224
217
225
``` js
226
+ const { URL } = require (' url' );
218
227
const idnURL = new URL (' https://你好你好' );
219
228
console .log (idnURL .origin );
220
229
// Prints https://你好你好
@@ -230,6 +239,7 @@ console.log(idnURL.hostname);
230
239
Gets and sets the password portion of the URL.
231
240
232
241
``` js
242
+ const { URL } = require (' url' );
233
243
const myURL = new URL (
' https://abc:[email protected] ' );
234
244
console .log (myURL .password );
235
245
// Prints xyz
@@ -251,6 +261,7 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
251
261
Gets and sets the path portion of the URL.
252
262
253
263
``` js
264
+ const { URL } = require (' url' );
254
265
const myURL = new URL (' https://example.org/abc/xyz?123' );
255
266
console .log (myURL .pathname );
256
267
// Prints /abc/xyz
@@ -272,6 +283,7 @@ to percent-encode may vary somewhat from what the [`url.parse()`][] and
272
283
Gets and sets the port portion of the URL.
273
284
274
285
``` js
286
+ const { URL } = require (' url' );
275
287
const myURL = new URL (' https://example.org:8888' );
276
288
console .log (myURL .port );
277
289
// Prints 8888
@@ -327,6 +339,7 @@ lies outside the range denoted above, it is ignored.
327
339
Gets and sets the protocol portion of the URL.
328
340
329
341
``` js
342
+ const { URL } = require (' url' );
330
343
const myURL = new URL (' https://example.org' );
331
344
console .log (myURL .protocol );
332
345
// Prints https:
@@ -345,6 +358,7 @@ Invalid URL protocol values assigned to the `protocol` property are ignored.
345
358
Gets and sets the serialized query portion of the URL.
346
359
347
360
``` js
361
+ const { URL } = require (' url' );
348
362
const myURL = new URL (' https://example.org/abc?123' );
349
363
console .log (myURL .search );
350
364
// Prints ?123
@@ -375,6 +389,7 @@ documentation for details.
375
389
Gets and sets the username portion of the URL.
376
390
377
391
``` js
392
+ const { URL } = require (' url' );
378
393
const myURL = new URL (
' https://abc:[email protected] ' );
379
394
console .log (myURL .username );
380
395
// Prints abc
@@ -412,6 +427,7 @@ This method is automatically called when an `URL` object is serialized
412
427
with [ ` JSON.stringify() ` ] [ ] .
413
428
414
429
``` js
430
+ const { URL } = require (' url' );
415
431
const myURLs = [
416
432
new URL (' https://www.example.com' ),
417
433
new URL (' https://test.example.org' )
@@ -696,6 +712,7 @@ with the same name is preserved.
696
712
This method can be used, in particular, to increase cache hits.
697
713
698
714
``` js
715
+ const { URLSearchParams } = require (' url' );
699
716
const params = new URLSearchParams (' query[]=abc&type=search&query[]=123' );
700
717
params .sort ();
701
718
console .log (params .toString ());
@@ -810,6 +827,7 @@ of the output.
810
827
For example:
811
828
812
829
``` js
830
+ const { URL } = require (' url' );
813
831
const myURL = new URL (' https://a:b@你好你好?abc#foo' );
814
832
815
833
console .log (myURL .href );
@@ -1043,6 +1061,7 @@ manner similar to that of a Web browser resolving an anchor tag HREF.
1043
1061
For example:
1044
1062
1045
1063
``` js
1064
+ const url = require (' url' );
1046
1065
url .resolve (' /one/two/three' , ' four' ); // '/one/two/four'
1047
1066
url .resolve (' http://example.com/' , ' /one' ); // 'http://example.com/one'
1048
1067
url .resolve (' http://example.com/one' , ' /two' ); // 'http://example.com/two'
0 commit comments