| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1362,6 +1362,24 @@ url.resolve('http://example.com/', '/one'); // 'http://example.com/one' | |||
| 1362 | 1362 | url.resolve('http://example.com/one', '/two'); // 'http://example.com/two' | |
| 1363 | 1363 | ``` | |
| 1364 | 1364 | ||
| 1365 | + You can achieve the same result using the WHATWG URL API: | ||
| 1366 | + | ||
| 1367 | + ```js | ||
| 1368 | + function resolve(from, to) { | ||
| 1369 | + const resolvedUrl = new URL(to, new URL(from, 'resolve://')); | ||
| 1370 | + if (resolvedUrl.protocol === 'resolve:') { | ||
| 1371 | + // `from` is a relative URL. | ||
| 1372 | + const { pathname, search, hash } = resolvedUrl; | ||
| 1373 | + return pathname + search + hash; | ||
| 1374 | + } | ||
| 1375 | + return resolvedUrl.toString(); | ||
| 1376 | + } | ||
| 1377 | + | ||
| 1378 | + resolve('/one/two/three', 'four'); // '/one/two/four' | ||
| 1379 | + resolve('http://example.com/', '/one'); // 'http://example.com/one' | ||
| 1380 | + resolve('http://example.com/one', '/two'); // 'http://example.com/two' | ||
| 1381 | + ``` | ||
| 1382 | + | ||
| 1365 | 1383 | <a id="whatwg-percent-encoding"></a> | |
| 1366 | 1384 | ## Percent-encoding in URLs | |
| 1367 | 1385 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments