@@ -92,6 +92,7 @@ Creates a new `URL` object by parsing the `input` relative to the `base`. If
9292` base ` is passed as a string, it will be parsed equivalent to ` new URL(base) ` .
9393
9494``` js
95+ const { URL } = require (' url' );
9596const myURL = new URL (' /foo' , ' https://example.org/' );
9697 // https://example.org/foo
9798```
@@ -101,6 +102,7 @@ that an effort will be made to coerce the given values into strings. For
101102instance:
102103
103104``` js
105+ const { URL } = require (' url' );
104106const myURL = new URL ({ toString : () => ' https://example.org/' });
105107 // https://example.org/
106108```
@@ -109,6 +111,7 @@ Unicode characters appearing within the hostname of `input` will be
109111automatically converted to ASCII using the [ Punycode] [ ] algorithm.
110112
111113``` js
114+ const { URL } = require (' url' );
112115const myURL = new URL (' https://你好你好' );
113116 // https://xn--6qqa088eba/
114117```
@@ -122,6 +125,7 @@ Additional [examples of parsed URLs][] may be found in the WHATWG URL Standard.
122125Gets and sets the fragment portion of the URL.
123126
124127``` js
128+ const { URL } = require (' url' );
125129const myURL = new URL (' https://example.org/foo#bar' );
126130console .log (myURL .hash );
127131 // Prints #bar
@@ -143,6 +147,7 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
143147Gets and sets the host portion of the URL.
144148
145149``` js
150+ const { URL } = require (' url' );
146151const myURL = new URL (' https://example.org:81/foo' );
147152console .log (myURL .host );
148153 // Prints example.org:81
@@ -163,6 +168,7 @@ Gets and sets the hostname portion of the URL. The key difference between
163168port.
164169
165170``` js
171+ const { URL } = require (' url' );
166172const myURL = new URL (' https://example.org:81/foo' );
167173console .log (myURL .hostname );
168174 // Prints example.org
@@ -181,6 +187,7 @@ Invalid hostname values assigned to the `hostname` property are ignored.
181187Gets and sets the serialized URL.
182188
183189``` js
190+ const { URL } = require (' url' );
184191const myURL = new URL (' https://example.org/foo' );
185192console .log (myURL .href );
186193 // Prints https://example.org/foo
@@ -209,12 +216,14 @@ may be contained within the hostname will be encoded as-is without [Punycode][]
209216encoding.
210217
211218``` js
219+ const { URL } = require (' url' );
212220const myURL = new URL (' https://example.org/foo/bar?baz' );
213221console .log (myURL .origin );
214222 // Prints https://example.org
215223```
216224
217225``` js
226+ const { URL } = require (' url' );
218227const idnURL = new URL (' https://你好你好' );
219228console .log (idnURL .origin );
220229 // Prints https://你好你好
@@ -230,6 +239,7 @@ console.log(idnURL.hostname);
230239Gets and sets the password portion of the URL.
231240
232241``` js
242+ const { URL } = require (' url' );
233243const myURL = new URL (' https://abc:xyz@example.com' );
234244console .log (myURL .password );
235245 // Prints xyz
@@ -251,6 +261,7 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
251261Gets and sets the path portion of the URL.
252262
253263``` js
264+ const { URL } = require (' url' );
254265const myURL = new URL (' https://example.org/abc/xyz?123' );
255266console .log (myURL .pathname );
256267 // Prints /abc/xyz
@@ -272,6 +283,7 @@ to percent-encode may vary somewhat from what the [`url.parse()`][] and
272283Gets and sets the port portion of the URL.
273284
274285``` js
286+ const { URL } = require (' url' );
275287const myURL = new URL (' https://example.org:8888' );
276288console .log (myURL .port );
277289 // Prints 8888
@@ -327,6 +339,7 @@ lies outside the range denoted above, it is ignored.
327339Gets and sets the protocol portion of the URL.
328340
329341``` js
342+ const { URL } = require (' url' );
330343const myURL = new URL (' https://example.org' );
331344console .log (myURL .protocol );
332345 // Prints https:
@@ -345,6 +358,7 @@ Invalid URL protocol values assigned to the `protocol` property are ignored.
345358Gets and sets the serialized query portion of the URL.
346359
347360``` js
361+ const { URL } = require (' url' );
348362const myURL = new URL (' https://example.org/abc?123' );
349363console .log (myURL .search );
350364 // Prints ?123
@@ -375,6 +389,7 @@ documentation for details.
375389Gets and sets the username portion of the URL.
376390
377391``` js
392+ const { URL } = require (' url' );
378393const myURL = new URL (' https://abc:xyz@example.com' );
379394console .log (myURL .username );
380395 // Prints abc
@@ -412,6 +427,7 @@ This method is automatically called when an `URL` object is serialized
412427with [ ` JSON.stringify() ` ] [ ] .
413428
414429``` js
430+ const { URL } = require (' url' );
415431const myURLs = [
416432 new URL (' https://www.example.com' ),
417433 new URL (' https://test.example.org' )
@@ -696,6 +712,7 @@ with the same name is preserved.
696712This method can be used, in particular, to increase cache hits.
697713
698714``` js
715+ const { URLSearchParams } = require (' url' );
699716const params = new URLSearchParams (' query[]=abc&type=search&query[]=123' );
700717params .sort ();
701718console .log (params .toString ());
@@ -810,6 +827,7 @@ of the output.
810827For example:
811828
812829``` js
830+ const { URL } = require (' url' );
813831const myURL = new URL (' https://a:b@你好你好?abc#foo' );
814832
815833console .log (myURL .href );
@@ -1043,6 +1061,7 @@ manner similar to that of a Web browser resolving an anchor tag HREF.
10431061For example:
10441062
10451063``` js
1064+ const url = require (' url' );
10461065url .resolve (' /one/two/three' , ' four' ); // '/one/two/four'
10471066url .resolve (' http://example.com/' , ' /one' ); // 'http://example.com/one'
10481067url .resolve (' http://example.com/one' , ' /two' ); // 'http://example.com/two'
0 commit comments