@@ -57,15 +57,10 @@ properties of a WHATWG `URL` object.
57
57
Parsing the URL string using the WHATWG API:
58
58
59
59
``` js
60
- const { URL } = require (' url' );
61
60
const myURL =
62
61
new URL (
' https://user:[email protected] :8080/p/a/t/h?query=string#hash' );
63
62
```
64
63
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
-
69
64
Parsing the URL string using the Legacy API:
70
65
71
66
``` js
@@ -75,14 +70,19 @@ const myURL =
75
70
```
76
71
77
72
## The WHATWG URL API
73
+
74
+ ### Class: URL
78
75
<!-- YAML
79
76
added: 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.
80
81
-->
81
82
82
- ### Class: URL
83
-
84
83
Browser-compatible ` URL ` class, implemented by following the WHATWG URL
85
84
Standard. [ Examples of parsed URLs] [ ] may be found in the Standard itself.
85
+ The ` URL ` class is also available on the global object.
86
86
87
87
* Note* : In accordance with browser conventions, all properties of ` URL ` objects
88
88
are 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
101
101
` base ` is passed as a string, it will be parsed equivalent to ` new URL(base) ` .
102
102
103
103
``` js
104
- const { URL } = require (' url' );
105
104
const myURL = new URL (' /foo' , ' https://example.org/' );
106
105
// https://example.org/foo
107
106
```
@@ -111,7 +110,6 @@ that an effort will be made to coerce the given values into strings. For
111
110
instance:
112
111
113
112
``` js
114
- const { URL } = require (' url' );
115
113
const myURL = new URL ({ toString : () => ' https://example.org/' });
116
114
// https://example.org/
117
115
```
@@ -120,7 +118,6 @@ Unicode characters appearing within the hostname of `input` will be
120
118
automatically converted to ASCII using the [ Punycode] [ ] algorithm.
121
119
122
120
``` js
123
- const { URL } = require (' url' );
124
121
const myURL = new URL (' https://你好你好' );
125
122
// https://xn--6qqa088eba/
126
123
```
@@ -135,7 +132,6 @@ with [ICU][] enabled. If not, the domain names are passed through unchanged.
135
132
Gets and sets the fragment portion of the URL.
136
133
137
134
``` js
138
- const { URL } = require (' url' );
139
135
const myURL = new URL (' https://example.org/foo#bar' );
140
136
console .log (myURL .hash );
141
137
// Prints #bar
@@ -157,7 +153,6 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
157
153
Gets and sets the host portion of the URL.
158
154
159
155
``` js
160
- const { URL } = require (' url' );
161
156
const myURL = new URL (' https://example.org:81/foo' );
162
157
console .log (myURL .host );
163
158
// Prints example.org:81
@@ -178,7 +173,6 @@ Gets and sets the hostname portion of the URL. The key difference between
178
173
port.
179
174
180
175
``` js
181
- const { URL } = require (' url' );
182
176
const myURL = new URL (' https://example.org:81/foo' );
183
177
console .log (myURL .hostname );
184
178
// Prints example.org
@@ -197,7 +191,6 @@ Invalid hostname values assigned to the `hostname` property are ignored.
197
191
Gets and sets the serialized URL.
198
192
199
193
``` js
200
- const { URL } = require (' url' );
201
194
const myURL = new URL (' https://example.org/foo' );
202
195
console .log (myURL .href );
203
196
// Prints https://example.org/foo
@@ -224,14 +217,12 @@ will be thrown.
224
217
Gets the read-only serialization of the URL's origin.
225
218
226
219
``` js
227
- const { URL } = require (' url' );
228
220
const myURL = new URL (' https://example.org/foo/bar?baz' );
229
221
console .log (myURL .origin );
230
222
// Prints https://example.org
231
223
```
232
224
233
225
``` js
234
- const { URL } = require (' url' );
235
226
const idnURL = new URL (' https://你好你好' );
236
227
console .log (idnURL .origin );
237
228
// Prints https://xn--6qqa088eba
@@ -247,7 +238,6 @@ console.log(idnURL.hostname);
247
238
Gets and sets the password portion of the URL.
248
239
249
240
``` js
250
- const { URL } = require (' url' );
251
241
const myURL = new URL (
' https://abc:[email protected] ' );
252
242
console .log (myURL .password );
253
243
// Prints xyz
@@ -269,7 +259,6 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
269
259
Gets and sets the path portion of the URL.
270
260
271
261
``` js
272
- const { URL } = require (' url' );
273
262
const myURL = new URL (' https://example.org/abc/xyz?123' );
274
263
console .log (myURL .pathname );
275
264
// Prints /abc/xyz
@@ -291,7 +280,6 @@ to percent-encode may vary somewhat from what the [`url.parse()`][] and
291
280
Gets and sets the port portion of the URL.
292
281
293
282
``` js
294
- const { URL } = require (' url' );
295
283
const myURL = new URL (' https://example.org:8888' );
296
284
console .log (myURL .port );
297
285
// Prints 8888
@@ -347,7 +335,6 @@ lies outside the range denoted above, it is ignored.
347
335
Gets and sets the protocol portion of the URL.
348
336
349
337
``` js
350
- const { URL } = require (' url' );
351
338
const myURL = new URL (' https://example.org' );
352
339
console .log (myURL .protocol );
353
340
// Prints https:
@@ -366,7 +353,6 @@ Invalid URL protocol values assigned to the `protocol` property are ignored.
366
353
Gets and sets the serialized query portion of the URL.
367
354
368
355
``` js
369
- const { URL } = require (' url' );
370
356
const myURL = new URL (' https://example.org/abc?123' );
371
357
console .log (myURL .search );
372
358
// Prints ?123
@@ -397,7 +383,6 @@ documentation for details.
397
383
Gets and sets the username portion of the URL.
398
384
399
385
``` js
400
- const { URL } = require (' url' );
401
386
const myURL = new URL (
' https://abc:[email protected] ' );
402
387
console .log (myURL .username );
403
388
// Prints abc
@@ -435,7 +420,6 @@ This method is automatically called when an `URL` object is serialized
435
420
with [ ` JSON.stringify() ` ] [ ] .
436
421
437
422
``` js
438
- const { URL } = require (' url' );
439
423
const myURLs = [
440
424
new URL (' https://www.example.com' ),
441
425
new URL (' https://test.example.org' )
@@ -447,20 +431,23 @@ console.log(JSON.stringify(myURLs));
447
431
### Class: URLSearchParams
448
432
<!-- YAML
449
433
added: 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.
450
438
-->
451
439
452
440
The ` URLSearchParams ` API provides read and write access to the query of a
453
441
` URL ` . The ` URLSearchParams ` class can also be used standalone with one of the
454
442
four following constructors.
443
+ The ` URLSearchParams ` class is also available on the global object.
455
444
456
445
The WHATWG ` URLSearchParams ` interface and the [ ` querystring ` ] [ ] module have
457
446
similar purpose, but the purpose of the [ ` querystring ` ] [ ] module is more
458
447
general, as it allows the customization of delimiter characters (` & ` and ` = ` ).
459
448
On the other hand, this API is designed purely for URL query strings.
460
449
461
450
``` js
462
- const { URL , URLSearchParams } = require (' url' );
463
-
464
451
const myURL = new URL (' https://example.org/?abc=123' );
465
452
console .log (myURL .searchParams .get (' abc' ));
466
453
// Prints 123
@@ -505,7 +492,6 @@ Parse the `string` as a query string, and use it to instantiate a new
505
492
` URLSearchParams ` object. A leading ` '?' ` , if present, is ignored.
506
493
507
494
``` js
508
- const { URLSearchParams } = require (' url' );
509
495
let params;
510
496
511
497
params = new URLSearchParams (' user=abc&query=xyz' );
@@ -534,7 +520,6 @@ values are not allowed. Arrays are stringified using [`array.toString()`][],
534
520
which simply joins all array elements with commas.
535
521
536
522
``` js
537
- const { URLSearchParams } = require (' url' );
538
523
const params = new URLSearchParams ({
539
524
user: ' abc' ,
540
525
query: [' first' , ' second' ]
@@ -562,7 +547,6 @@ themselves be any iterable object.
562
547
Duplicate keys are allowed.
563
548
564
549
``` js
565
- const { URLSearchParams } = require (' url' );
566
550
let params;
567
551
568
552
// Using an array
@@ -631,7 +615,6 @@ Alias for [`urlSearchParams[@@iterator]()`][`urlSearchParams@@iterator()`].
631
615
Iterates over each name-value pair in the query and invokes the given function.
632
616
633
617
``` js
634
- const { URL } = require (' url' );
635
618
const myURL = new URL (' https://example.org/?a=b&c=d' );
636
619
myURL .searchParams .forEach ((value , name , searchParams ) => {
637
620
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`.
672
655
Returns an ES6 Iterator over the names of each name-value pair.
673
656
674
657
``` js
675
- const { URLSearchParams } = require (' url' );
676
658
const params = new URLSearchParams (' foo=bar&foo=baz' );
677
659
for (const name of params .keys ()) {
678
660
console .log (name);
@@ -693,8 +675,6 @@ set the first such pair's value to `value` and remove all others. If not,
693
675
append the name-value pair to the query string.
694
676
695
677
``` js
696
- const { URLSearchParams } = require (' url' );
697
-
698
678
const params = new URLSearchParams ();
699
679
params .append (' foo' , ' bar' );
700
680
params .append (' foo' , ' baz' );
@@ -720,7 +700,6 @@ with the same name is preserved.
720
700
This method can be used, in particular, to increase cache hits.
721
701
722
702
``` js
723
- const { URLSearchParams } = require (' url' );
724
703
const params = new URLSearchParams (' query[]=abc&type=search&query[]=123' );
725
704
params .sort ();
726
705
console .log (params .toString ());
@@ -751,7 +730,6 @@ is the `name`, the second item of the Array is the `value`.
751
730
Alias for [ ` urlSearchParams.entries() ` ] [ ] .
752
731
753
732
``` js
754
- const { URLSearchParams } = require (' url' );
755
733
const params = new URLSearchParams (' foo=bar&xyz=baz' );
756
734
for (const [name , value ] of params) {
757
735
console .log (name, value);
@@ -835,7 +813,6 @@ of the output.
835
813
For example:
836
814
837
815
``` js
838
- const { URL } = require (' url' );
839
816
const myURL = new URL (' https://a:b@你好你好?abc#foo' );
840
817
841
818
console .log (myURL .href );
@@ -1135,7 +1112,6 @@ using the [Punycode][] algorithm. Note, however, that a hostname *may* contain
1135
1112
* both* Punycode encoded and percent-encoded characters. For example:
1136
1113
1137
1114
``` js
1138
- const { URL } = require (' url' );
1139
1115
const myURL = new URL (' https://%CF%80.com/foo' );
1140
1116
console .log (myURL .href );
1141
1117
// Prints https://xn--1xa.com/foo
0 commit comments