Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Location: fragment, origin, path, query, and full URL

Read the five URL properties of a browser document and distinguish navigation-triggering assignments from the read-only origin.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

const loc = window.location;
loc.hash;      // string: fragment
loc.origin;    // string: URL origin (read-only)
loc.pathname;  // string: path
loc.search;    // string: query
loc.href;      // string: complete URL

// Assignments on a live Location can initiate navigation:
loc.hash = "intro";
loc.pathname = "/guide";
loc.search = "?mode=demo";
loc.href = "/guide?mode=demo#intro";

Location.hash/origin/pathname/search/href names five properties on the Location object returned by window.location. This is a browser Web API, not part of ECMAScript 2024. The object reflects the document’s current URL; a string you read earlier does not update. Use new URL(window.location.href) to inspect or edit a separate URL without navigating.

href is the full serialized URL. pathname is only the path, without the query or fragment. On HTTP(S) it starts with /, even at the root; an opaque path such as blank in about:blank need not start with a slash. search returns a nonempty query with ?, and hash a nonempty fragment with #. Each returns "" if its component is absent or empty, even if href still shows the bare delimiter. Reading does not percent-decode these strings. Use new URLSearchParams(window.location.search) to read individual query parameters.

origin is read-only and describes the URL’s origin. For HTTP(S), it contains the scheme and hostname, plus the port if it is non-default. An opaque URL origin serializes as the string "null", not the value null. This is not the same as self.origin, which describes the execution environment’s origin: an about:blank document can inherit that origin. Matching "null" strings do not establish that two origins are the same.

Assigning to href can initiate navigation to a new URL, including a relative one; if parsing fails, it throws a SyntaxError DOMException. Assigning to pathname or search can initiate navigation with a changed path or query. Writing pathname does nothing for an opaque path. The leading ? and # are optional when setting search and hash, respectively. search = "" removes the query, but hash = "" can leave a trailing #. Changing only the fragment usually does not load a new document.

Reading any of the five properties on a cross-origin window can throw a SecurityError DOMException. Writing href is an exception: it can be allowed without read access, but browser navigation restrictions still apply.

Examples

const { hash, origin, pathname, search, href } = new URL("about:blank?mode=demo#intro");
console.log(JSON.stringify({ hash, origin, pathname, search, href }));

Result

{"hash":"#intro","origin":"null","pathname":"blank","search":"?mode=demo","href":"about:blank?mode=demo#intro"}

Run this in Node.js. The example reads the same URL components from a `URL` object rather than a browser `Location`, so it does not navigate. In the output, `"null"` is a string and `blank` has no leading `/`.

Related lessons

  • JavaScript · Lesson 38 · Advanced · Version: 4

Sources