GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — doc: add url.resolve replacement example · nodejs/node@ff1990c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • url.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/url.md‎

Copy file name to clipboardExpand all lines: doc/api/url.md
+18Lines changed: 18 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,24 @@ url.resolve('http://example.com/', '/one'); // 'http://example.com/one'
13001300
url.resolve('http://example.com/one', '/two'); // 'http://example.com/two'
13011301
```
13021302

1303+
You can achieve the same result using the WHATWG URL API:
1304+
1305+
```js
1306+
function resolve(from, to) {
1307+
const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
1308+
if (resolvedUrl.protocol === 'resolve:') {
1309+
// `from` is a relative URL.
1310+
const { pathname, search, hash } = resolvedUrl;
1311+
return pathname + search + hash;
1312+
}
1313+
return resolvedUrl.toString();
1314+
}
1315+
1316+
resolve('/one/two/three', 'four'); // '/one/two/four'
1317+
resolve('http://example.com/', '/one'); // 'http://example.com/one'
1318+
resolve('http://example.com/one', '/two'); // 'http://example.com/two'
1319+
```
1320+
13031321
<a id="whatwg-percent-encoding"></a>
13041322
## Percent-encoding in URLs
13051323

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.