@@ -57,15 +57,10 @@ properties of a WHATWG `URL` object.
5757Parsing the URL string using the WHATWG API:
5858
5959``` js
60- const { URL } = require (' url' );
6160const myURL =
6261 new URL (
' https://user:[email protected] :8080/p/a/t/h?query=string#hash' );
6362```
6463
65- * Note* : In Web Browsers, the WHATWG ` URL ` class is a global that is always
66- available. In Node.js, however, the ` URL ` class must be accessed via
67- ` require('url').URL ` .
68-
6964Parsing the URL string using the Legacy API:
7065
7166``` js
@@ -75,14 +70,19 @@ const myURL =
7570```
7671
7772## The WHATWG URL API
73+
74+ ### Class: URL
7875<!-- YAML
7976added: v7.0.0
77+ changes:
78+ - version: REPLACEME
79+ pr-url: https://github.com/nodejs/node/pull/18281
80+ description: The class is now available on the global object.
8081-->
8182
82- ### Class: URL
83-
8483Browser-compatible ` URL ` class, implemented by following the WHATWG URL
8584Standard. [ Examples of parsed URLs] [ ] may be found in the Standard itself.
85+ The ` URL ` class is also available on the global object.
8686
8787* Note* : In accordance with browser conventions, all properties of ` URL ` objects
8888are implemented as getters and setters on the class prototype, rather than as
@@ -101,7 +101,6 @@ Creates a new `URL` object by parsing the `input` relative to the `base`. If
101101` base ` is passed as a string, it will be parsed equivalent to ` new URL(base) ` .
102102
103103``` js
104- const { URL } = require (' url' );
105104const myURL = new URL (' /foo' , ' https://example.org/' );
106105// https://example.org/foo
107106```
@@ -111,7 +110,6 @@ that an effort will be made to coerce the given values into strings. For
111110instance:
112111
113112``` js
114- const { URL } = require (' url' );
115113const myURL = new URL ({ toString : () => ' https://example.org/' });
116114// https://example.org/
117115```
@@ -120,7 +118,6 @@ Unicode characters appearing within the hostname of `input` will be
120118automatically converted to ASCII using the [ Punycode] [ ] algorithm.
121119
122120``` js
123- const { URL } = require (' url' );
124121const myURL = new URL (' https://你好你好' );
125122// https://xn--6qqa088eba/
126123```
@@ -135,7 +132,6 @@ with [ICU][] enabled. If not, the domain names are passed through unchanged.
135132Gets and sets the fragment portion of the URL.
136133
137134``` js
138- const { URL } = require (' url' );
139135const myURL = new URL (' https://example.org/foo#bar' );
140136console .log (myURL .hash );
141137// Prints #bar
@@ -157,7 +153,6 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
157153Gets and sets the host portion of the URL.
158154
159155``` js
160- const { URL } = require (' url' );
161156const myURL = new URL (' https://example.org:81/foo' );
162157console .log (myURL .host );
163158// Prints example.org:81
@@ -178,7 +173,6 @@ Gets and sets the hostname portion of the URL. The key difference between
178173port.
179174
180175``` js
181- const { URL } = require (' url' );
182176const myURL = new URL (' https://example.org:81/foo' );
183177console .log (myURL .hostname );
184178// Prints example.org
@@ -197,7 +191,6 @@ Invalid hostname values assigned to the `hostname` property are ignored.
197191Gets and sets the serialized URL.
198192
199193``` js
200- const { URL } = require (' url' );
201194const myURL = new URL (' https://example.org/foo' );
202195console .log (myURL .href );
203196// Prints https://example.org/foo
@@ -224,14 +217,12 @@ will be thrown.
224217Gets the read-only serialization of the URL's origin.
225218
226219``` js
227- const { URL } = require (' url' );
228220const myURL = new URL (' https://example.org/foo/bar?baz' );
229221console .log (myURL .origin );
230222// Prints https://example.org
231223```
232224
233225``` js
234- const { URL } = require (' url' );
235226const idnURL = new URL (' https://你好你好' );
236227console .log (idnURL .origin );
237228// Prints https://xn--6qqa088eba
@@ -247,7 +238,6 @@ console.log(idnURL.hostname);
247238Gets and sets the password portion of the URL.
248239
249240``` js
250- const { URL } = require (' url' );
251241const myURL = new URL (
' https://abc:[email protected] ' );
252242console .log (myURL .password );
253243// Prints xyz
@@ -269,7 +259,6 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
269259Gets and sets the path portion of the URL.
270260
271261``` js
272- const { URL } = require (' url' );
273262const myURL = new URL (' https://example.org/abc/xyz?123' );
274263console .log (myURL .pathname );
275264// Prints /abc/xyz
@@ -291,7 +280,6 @@ to percent-encode may vary somewhat from what the [`url.parse()`][] and
291280Gets and sets the port portion of the URL.
292281
293282``` js
294- const { URL } = require (' url' );
295283const myURL = new URL (' https://example.org:8888' );
296284console .log (myURL .port );
297285// Prints 8888
@@ -347,7 +335,6 @@ lies outside the range denoted above, it is ignored.
347335Gets and sets the protocol portion of the URL.
348336
349337``` js
350- const { URL } = require (' url' );
351338const myURL = new URL (' https://example.org' );
352339console .log (myURL .protocol );
353340// Prints https:
@@ -366,7 +353,6 @@ Invalid URL protocol values assigned to the `protocol` property are ignored.
366353Gets and sets the serialized query portion of the URL.
367354
368355``` js
369- const { URL } = require (' url' );
370356const myURL = new URL (' https://example.org/abc?123' );
371357console .log (myURL .search );
372358// Prints ?123
@@ -397,7 +383,6 @@ documentation for details.
397383Gets and sets the username portion of the URL.
398384
399385``` js
400- const { URL } = require (' url' );
401386const myURL = new URL (
' https://abc:[email protected] ' );
402387console .log (myURL .username );
403388// Prints abc
@@ -435,7 +420,6 @@ This method is automatically called when an `URL` object is serialized
435420with [ ` JSON.stringify() ` ] [ ] .
436421
437422``` js
438- const { URL } = require (' url' );
439423const myURLs = [
440424 new URL (' https://www.example.com' ),
441425 new URL (' https://test.example.org' )
@@ -447,20 +431,23 @@ console.log(JSON.stringify(myURLs));
447431### Class: URLSearchParams
448432<!-- YAML
449433added: v7.5.0
434+ changes:
435+ - version: REPLACEME
436+ pr-url: https://github.com/nodejs/node/pull/18281
437+ description: The class is now available on the global object.
450438-->
451439
452440The ` URLSearchParams ` API provides read and write access to the query of a
453441` URL ` . The ` URLSearchParams ` class can also be used standalone with one of the
454442four following constructors.
443+ The ` URLSearchParams ` class is also available on the global object.
455444
456445The WHATWG ` URLSearchParams ` interface and the [ ` querystring ` ] [ ] module have
457446similar purpose, but the purpose of the [ ` querystring ` ] [ ] module is more
458447general, as it allows the customization of delimiter characters (` & ` and ` = ` ).
459448On the other hand, this API is designed purely for URL query strings.
460449
461450``` js
462- const { URL , URLSearchParams } = require (' url' );
463-
464451const myURL = new URL (' https://example.org/?abc=123' );
465452console .log (myURL .searchParams .get (' abc' ));
466453// Prints 123
@@ -505,7 +492,6 @@ Parse the `string` as a query string, and use it to instantiate a new
505492` URLSearchParams ` object. A leading ` '?' ` , if present, is ignored.
506493
507494``` js
508- const { URLSearchParams } = require (' url' );
509495let params;
510496
511497params = new URLSearchParams (' user=abc&query=xyz' );
@@ -534,7 +520,6 @@ values are not allowed. Arrays are stringified using [`array.toString()`][],
534520which simply joins all array elements with commas.
535521
536522``` js
537- const { URLSearchParams } = require (' url' );
538523const params = new URLSearchParams ({
539524 user: ' abc' ,
540525 query: [' first' , ' second' ]
@@ -562,7 +547,6 @@ themselves be any iterable object.
562547Duplicate keys are allowed.
563548
564549``` js
565- const { URLSearchParams } = require (' url' );
566550let params;
567551
568552// Using an array
@@ -631,7 +615,6 @@ Alias for [`urlSearchParams[@@iterator]()`][`urlSearchParams@@iterator()`].
631615Iterates over each name-value pair in the query and invokes the given function.
632616
633617``` js
634- const { URL } = require (' url' );
635618const myURL = new URL (' https://example.org/?a=b&c=d' );
636619myURL .searchParams .forEach ((value , name , searchParams ) => {
637620 console .log (name, value, myURL .searchParams === searchParams);
@@ -672,7 +655,6 @@ Returns `true` if there is at least one name-value pair whose name is `name`.
672655Returns an ES6 Iterator over the names of each name-value pair.
673656
674657``` js
675- const { URLSearchParams } = require (' url' );
676658const params = new URLSearchParams (' foo=bar&foo=baz' );
677659for (const name of params .keys ()) {
678660 console .log (name);
@@ -693,8 +675,6 @@ set the first such pair's value to `value` and remove all others. If not,
693675append the name-value pair to the query string.
694676
695677``` js
696- const { URLSearchParams } = require (' url' );
697-
698678const params = new URLSearchParams ();
699679params .append (' foo' , ' bar' );
700680params .append (' foo' , ' baz' );
@@ -720,7 +700,6 @@ with the same name is preserved.
720700This method can be used, in particular, to increase cache hits.
721701
722702``` js
723- const { URLSearchParams } = require (' url' );
724703const params = new URLSearchParams (' query[]=abc&type=search&query[]=123' );
725704params .sort ();
726705console .log (params .toString ());
@@ -751,7 +730,6 @@ is the `name`, the second item of the Array is the `value`.
751730Alias for [ ` urlSearchParams.entries() ` ] [ ] .
752731
753732``` js
754- const { URLSearchParams } = require (' url' );
755733const params = new URLSearchParams (' foo=bar&xyz=baz' );
756734for (const [name , value ] of params) {
757735 console .log (name, value);
@@ -835,7 +813,6 @@ of the output.
835813For example:
836814
837815``` js
838- const { URL } = require (' url' );
839816const myURL = new URL (' https://a:b@你好你好?abc#foo' );
840817
841818console .log (myURL .href );
@@ -1135,7 +1112,6 @@ using the [Punycode][] algorithm. Note, however, that a hostname *may* contain
11351112* both* Punycode encoded and percent-encoded characters. For example:
11361113
11371114``` js
1138- const { URL } = require (' url' );
11391115const myURL = new URL (' https://%CF%80.com/foo' );
11401116console .log (myURL .href );
11411117// Prints https://xn--1xa.com/foo
0 commit comments