CKTIS / JavaScript
Location.hash and Location.href: fragment versus full URL
`Location.hash` reads or changes the URL fragment. `Location.href` returns the full URL or requests navigation to a destination.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const fragment = window.location.hash;
const currentURL = window.location.href;
window.location.hash = fragmentString;
window.location.href = destinationURL;Location.hash/href refers to two properties of the live Location object exposed by window.location. They are browser APIs defined by the HTML Standard, not features of ECMAScript 2024. Do not assume they exist outside a browser window context.
Reading hash returns a nonempty fragment with its leading #, without percent-decoding it. If the fragment is missing or empty, it returns "". Reading href returns the entire URL as a string, including the query and fragment if present. A bare trailing # can appear in href even when hash is "".
Setting hash changes only the fragment; the leading # is optional. If the resulting fragment is unchanged, the setter does not navigate. Assigning "" sets an empty fragment rather than removing it. A fragment change through same-document navigation fires hashchange without reloading the document.
Setting href specifies a navigation destination. A relative URL is resolved against the calling environment's base URL. If parsing fails, the browser throws a DOMException named SyntaxError. Origin rules restrict reading either property and writing hash. Writing href can be allowed across origins, but navigation restrictions still apply.
Examples
(async () => {
const show = address => console.log(JSON.stringify([address.hash, address.href]));
if (typeof document === 'undefined') {
// Node.js: URL serialization only, without navigation or hashchange.
const url = new URL('about:blank');
show(url);
url.hash = 'details';
show(url);
url.href = 'about:blank#summary';
show(url);
return;
}
const frame = document.createElement('iframe');
frame.hidden = true;
document.body.append(frame);
try {
const child = frame.contentWindow;
const location = child.location;
const nextHashChange = () => new Promise(resolve => {
child.addEventListener('hashchange', resolve, { once: true });
});
show(location);
const firstChange = nextHashChange();
location.hash = 'details';
await firstChange;
show(location);
const secondChange = nextHashChange();
location.href = 'about:blank#summary';
await secondChange;
show(location);
} finally {
frame.remove();
}
})();Result
["","about:blank"] ["#details","about:blank#details"] ["#summary","about:blank#summary"]
Run this in the console of an ordinary, fully loaded HTTP(S) page that allows an unsandboxed iframe. Each line shows `[hash, href]`. Both assignments change the iframe's URL, not the parent page's URL. In Node.js, the example uses a `URL` object instead of `Location` to show serialization only, without navigation or `hashchange` events.
Related lessons
- JavaScript · Lesson 30 · Advanced · Version: 4