Skip to content

Commit 30bc2e4

Browse files
jasnellruyadorno
authored andcommitted
doc: add examples for WHATWG URL objects
Signed-off-by: James M Snell <[email protected]> PR-URL: #37822 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 5d4c610 commit 30bc2e4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

doc/api/url.md

+25
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,31 @@ const myURL =
6767
url.parse('https://user:[email protected]:8080/p/a/t/h?query=string#hash');
6868
```
6969

70+
### Constructing a URL from component parts and getting the constructed string
71+
72+
It is possible to construct a WHATWG URL from component parts using either the
73+
property setters or a template literal string:
74+
75+
```js
76+
const myURL = new URL('https://example.org');
77+
myURL.pathname = '/a/b/c';
78+
myURL.search = '?d=e';
79+
myURL.hash = '#fgh';
80+
```
81+
82+
```js
83+
const pathname = '/a/b/c';
84+
const search = '?d=e';
85+
const hash = '#fgh';
86+
const myURL = new URL(`https://example.org${pathname}${search}${hash}`);
87+
```
88+
89+
To get the constructed URL string, use the `href` property accessor:
90+
91+
```js
92+
console.log(myURL.href);
93+
```
94+
7095
## The WHATWG URL API
7196

7297
### Class: `URL`

0 commit comments

Comments
 (0)