Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Location.href — reading the address and navigating

Location.href exposes the document URL as a string. Assigning a value initiates navigation rather than merely changing an ordinary variable.

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.href
window.location.href = url

Location.href is a browser API property accessed through window.location.href. Reading it returns the document's serialized URL, including its query and fragment when present.

Assigning window.location.href = url initiates navigation. The setter resolves a relative address against the entry settings object's API base URL. In a synchronous call between frames, this can be the base URL of the frame that initiated the call rather than that of the frame whose code performs the assignment. An invalid address can throw a SyntaxError DOMException.

Reading another window's address is subject to origin restrictions and can throw SecurityError. The example only reads the address; it does not leave the page.

Examples

const href = window.location.href;
console.log(typeof href);
console.log(href === window.location.toString());

Result

string
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources