resolve-pathname resolves URL pathnames identical to the way browsers resolve the pathname of an <a href>
value. The goals are:
- 100% compatibility with browser pathname resolution
- Pure JavaScript implementation (no DOM dependency)
Installation
Using npm:
$ npm install --save resolve-pathname
Then, use as you would anything else:
// using ES6 modules; // using CommonJS modulesvar resolvePathname = ;
The UMD build is also available on unpkg:
You can find the library on window.resolvePathname
.
Usage
; // Simply pass the pathname you'd like to resolve. Second// argument is the path we're coming from, or the current// pathname. It defaults to "/".; // /company/about; // /company/jobs; // /about; // /about // Index paths (with a trailing slash) are also supported and// work the same way as browsers.; // /company/info/about // In browsers, it's easy to resolve a URL pathname relative to// the current page. Just use window.location! e.g. if// window.location.pathname == '/company/team/ceo' then; // /company/team/cto; // /company/jobs
Prior Work
- url.resolve - node's
url.resolve
implementation for full URLs - resolve-url - A DOM-dependent implementation of the same algorithm