Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Location.hash — the page URL fragment

Location.hash reads and changes the current page's URL fragment. A nonempty value read from it includes the leading # character.

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.hash
window.location.hash = "section"
window.location.hash = "#section"

Location.hash, available as window.location.hash, returns the fragment with a leading #. Both an absent fragment and an empty fragment produce "".

Assignment accepts a value with or without # and initiates fragment navigation. Assigning the same fragment does nothing. Reading does not decode percent-encoded sequences.

Warning: assigning "" sets an empty fragment rather than guaranteeing removal of the URL's trailing #. Accessing hash on a cross-origin page can throw SecurityError.

Examples

window.location.hash = "cktis-example";
console.log(window.location.hash);
window.location.hash = "";
console.log(JSON.stringify(window.location.hash));

Result

#cktis-example
""

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