Skip to content

Commit

Permalink
doc: add examples for WHATWG URL objects
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jasnell authored and ruyadorno committed Mar 24, 2021
1 parent 5d4c610 commit 30bc2e4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ const myURL =
url.parse('https://user:[email protected]:8080/p/a/t/h?query=string#hash');
```

### Constructing a URL from component parts and getting the constructed string

It is possible to construct a WHATWG URL from component parts using either the
property setters or a template literal string:

```js
const myURL = new URL('https://example.org');
myURL.pathname = '/a/b/c';
myURL.search = '?d=e';
myURL.hash = '#fgh';
```

```js
const pathname = '/a/b/c';
const search = '?d=e';
const hash = '#fgh';
const myURL = new URL(`https://example.org${pathname}${search}${hash}`);
```

To get the constructed URL string, use the `href` property accessor:

```js
console.log(myURL.href);
```

## The WHATWG URL API

### Class: `URL`
Expand Down

0 comments on commit 30bc2e4

Please sign in to comment.