From 77891590099bc49ea184f61f1831fd56fa4f3bc7 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 19 Mar 2021 14:43:18 -0700 Subject: [PATCH] doc: add examples for WHATWG URL objects Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/37822 Reviewed-By: Anna Henningsen Reviewed-By: Myles Borins Reviewed-By: Darshan Sen --- doc/api/url.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/doc/api/url.md b/doc/api/url.md index d7191504567910..b5d1c95f3cb6ba 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -67,6 +67,31 @@ const myURL = url.parse('https://user:pass@sub.example.com: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`