CKTIS / JavaScript
URL.pathname — reading and changing the path
URL.pathname lets you read or replace a URL's path without changing its query or fragment.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
url.pathname
url.pathname = pathURL.pathname is an instance property of URL, not a static constructor property. It returns the serialized path without the query or fragment. For an HTTPS URL without path segments, it returns /.
Assignment replaces the path, preserving search and hash. Spaces become %20; reading does not automatically decode percent-encoded sequences. Changing the object does not navigate the browser.
Opaque paths, such as in mailto:person@example.org, can be read, but assignment to pathname is ignored.
Examples
const url = new URL('https://example.org/docs/start?q=1#intro');
console.log(url.pathname);
url.pathname = '/learn JavaScript';
console.log(url.pathname);
console.log(url.href);Result
/docs/start /learn%20JavaScript https://example.org/learn%20JavaScript?q=1#intro
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.