The onhashchange attribute is a window-level event-handler (also allowed on <body>). It runs JavaScript when the hashchange event fires — the URL fragment identifier changed (the part starting with #).
Typical triggers: clicking an in-page link like <a href="#about">, the Back/Forward buttons after hash navigations, or assigning location.hash in script. Related: onpopstate (History API) and onload (handle the initial hash).
Baseline · Widely available.
Not deprecated and not experimental. Per MDN, hashchange does not fire when the hash is changed with history.pushState() or history.replaceState(). Stable since about July 2015.
Discouraged as inline HTML.
MDN discourages HTML event handler attributes. Prefer window.addEventListener("hashchange", handler) in production; use <body onhashchange> for quick demos and learning.
Syntax
HTML Syntax
syntax.html
<body onhashchange="script">
script is JavaScript that runs when the hash changes. Equivalent JS: window.onhashchange = handler or window.addEventListener("hashchange", handler).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Still use location.hash when you only need the fragment string.
Edge Cases & Pitfalls
Most “hashchange never ran” bugs are expecting it on first load or after pushState.
load
No fire on first load
If the user opens page.html#about, call your render function once on load (or immediately in a script). hashchange only runs on later changes.
history
pushState / replaceState
Per MDN, changing the hash via history.pushState() or replaceState() does not fire hashchange. Prefer onpopstate for History API apps.
target
Window / body only
This is not an input event. Put the handler on window or <body onhashchange>, not on a button or field.
scroll
Same-page anchors scroll
A matching id on the page still scrolls into view. Use unique demo hashes (or preventDefault on clicks) when you only want routing, not scrolling.
spa
Hash routing vs History API
Hash routes work without a server rewrite. Clean URLs usually use pushState + popstate instead of # fragments.
Compatibility
Browser Support
onhashchange is a window / body event-handler in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
onhashchange
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support hashchange on window.
97%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet Explorer8+
Supported
onhashchangeWidely supported
Bottom line: Safe to use for hash-based UI. Prefer addEventListener('hashchange') in apps, sync once on load, and use popstate for pushState routing.
Remember
Key Takeaways
Role: the URL #fragment changed.
Window / body handler — not for form controls.
No initial fire — sync once on load for the starting hash.
Not for pushState — use popstate with the History API.
One line:onhashchange means the # part of the URL changed — listen on window, and render once on load too.
Frequently Asked Questions
It is a window-level event-handler attribute (also allowed on body). When the URL fragment identifier (the part starting with #) changes, the hashchange event fires and the JavaScript in onhashchange runs.
On the window (window.onhashchange) or as an attribute on body (and historically frameset). It is not for input or button elements.
No. It only fires when the hash changes after load. Call your sync/render function once on load to handle an initial hash already in the URL.
Per MDN, hashchange does not fire when the hash is modified with history.pushState() or history.replaceState(). Those navigations relate more to popstate.
hashchange is specifically for location.hash changes. popstate fires for session history navigations involving pushState/replaceState (and some back/forward cases). Modern History API apps often use popstate instead of hash routing.
No. The hashchange event and onhashchange handler are standard and Baseline widely available since about July 2015. Inline HTML handlers are discouraged in production in favor of addEventListener.