The hashchange event fires when a window's hash changes (see location.hash
).
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);
funcRef
if ("onhashchange" in window) { alert("The browser supports the hashchange event!"); } function locationHashChanged() { if (location.hash === "#somecoolfeature") { somecoolfeature(); } } window.onhashchange = locationHashChanged;
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. |
//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; }); }());
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'GlobalEventHandlers' 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 |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 5 | (Yes) | 3.6 (1.9.2) | 8.0
| 10.6 | 5.0 |
Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | 1.0 (1.9.2) | 9.0 | 11.0 | 5.0 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onhashchange