The hashchange event fires when a window's hash changes (see location.hash).
Syntax
window.onhashchange = funcRef;
or
<body onhashchange="funcRef();">
to overwrite any existing Event-Handlers.
In order to add an event-listener to an existing set of event-handlers use the function "addEventListener"
window.addEventListener("hashchange", funcRef, false);
Parameters
funcRef- A reference to a function.
Example
if ("onhashchange" in window) {
//no alert
console.log("The browser supports the hashchange event!");
}
function locationHashChanged() {
if (location.hash === "#somecoolfeature") {
somecoolfeature();
}
}
window.onhashchange = locationHashChanged;
// example
// Using the location.hash property to change the anchor part
function changeHash() {
location.hash = (Math.random() > 0.5) ? "666" : "777";
}
// Alert some text if there has been changes to the anchor part
function HashHandler() {
console.log("The Hash has changed!");
}
window.addEventListener("hashchange", HashHandler, false);
The hashchange event
The dispatched hashchange event has the following fields:
| Field | Type | Description |
newURL |
DOMString |
The new URL to which the window is navigating. |
oldURL |
DOMString |
The previous URL from which the window was navigated. |
Workaround for event.newURL and event.oldURL
//let this snippet run before your hashchange event binding code
if(!window.HashChangeEvent)(function(){
var lastURL=document.URL;
window.addEventListener("hashchange",function(event){
Object.defineProperty(event,"oldURL",{enumerable:true,configurable:true,value:lastURL});
Object.defineProperty(event,"newURL",{enumerable:true,configurable:true,value:document.URL});
lastURL=document.URL;
});
}());
Specifications
| Specification | Status | Comment |
|---|---|---|
| HTML Living Standard The definition of 'onhashchange' in that specification. |
Living Standard | |
| HTML 5.1 The definition of 'GlobalEventHandlers' in that specification. |
Recommendation | |
| HTML5 The definition of 'GlobalEventHandlers' in that specification. |
Recommendation |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Chrome Full support 5 | Edge Full support Yes | Firefox Full support 3.6 | IE Full support 8 | Opera Full support 10 | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support 11 | Safari iOS Full support 5 | Samsung Internet Android ? |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
Document Tags and Contributors
Tags:
Contributors to this page:
sideshowbarker,
carolinekabat,
jcamenisch,
xgqfrms,
fscholz,
douira,
jpmedley,
tobias74,
erikadoyle,
fuweichin,
teoli,
Taken,
libreGeekingKid,
leon_gilyadov,
Sheppy,
ethertank,
ziyunfei,
McGurk,
Jürgen Jeka,
Crash,
Nickolay,
jlebar,
Sephr
Last updated by:
sideshowbarker,