CKTIS / JavaScript
Page URL components: origin, pathname, search, hash, and href
The window.location object exposes the current page's complete URL and its components. Learn how origin, pathname, search, hash, and href differ, and why writing to Location can trigger navigation.
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
window.location.origin
window.location.pathname
window.location.search
window.location.hash
window.location.hrefwindow.location is a Location object associated with the current document; it is also accessible as document.location. Its properties describe the page URL and return strings. This is a browser Web API, not a feature of ECMAScript itself. The presence of Node 24 in a repository does not establish support for the browser's Location.
Location.origin is read-only. For HTTP(S), it contains the scheme, //, hostname, and port if non-default; it excludes the path. For file:, the result depends on the browser, so do not generalize HTTP(S) rules to every scheme. Location.pathname contains the path without the query or fragment; for HTTP(S), it starts with /.
Location.search returns a non-empty query with its leading ?. If the query is absent or empty, it returns "", including when the URL ends in a bare ?. Reading it does not percent-decode sequences: %20 remains %20. Use new URLSearchParams(window.location.search) to read individual parameters. Location.hash returns a non-empty fragment with its leading #. If the fragment is absent or empty, it returns "", including when the URL ends in a bare #. Location.href returns the entire URL, including the query and fragment.
Reading these properties does not change the page, but writing to href triggers navigation. Changing pathname or search can also load a document; changing only hash usually means same-document navigation. To safely inspect an arbitrary address, use URL rather than modifying window.location. The example uses a separate URL object with corresponding property names: its output is fixed and it does not redirect the page. It is not a test of Location navigation behavior.
Examples
const url = new URL("https://example.org:8443/docs/start?q=web%20api#intro");
for (const key of ["origin", "pathname", "search", "hash", "href"]) {
console.log(`${key}: ${url[key]}`);
}Result
origin: https://example.org:8443 pathname: /docs/start search: ?q=web%20api hash: #intro href: https://example.org:8443/docs/start?q=web%20api#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.