The hashchange event is fired when the fragment identifier of the URL has changed (the part of the URL beginning with and following the # symbol).
| Bubbles | Yes |
| Cancelable | No |
| Interface | HashChangeEvent
|
| Event handler | onhashchange
|
Examples
You can use the hashchange event in an addEventListener method:
window.addEventListener('hashchange', function() {
console.log('The hash has changed!')
}, false);
Or use the onhashchange event handler property:
function locationHashChanged() {
if (location.hash === '#cool-feature') {
console.log("You're visiting a cool feature!");
}
}
window.onhashchange = locationHashChanged;
Specifications
| Specification | Status | Comment |
|---|---|---|
| HTML Living StandardThe definition of 'hashchange' in that specification. | Living Standard | Initial definition |
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 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
hashchange event
|
Chrome
Full support 5 |
Edge
Full support 12 |
Firefox
Full support 3.6 |
IE
Full support 8 |
Opera
Full support 10.6 |
Safari
Full support 5 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 11 |
Safari iOS
Full support 5 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
See also
Window: hashchange event by Mozilla Contributors is licensed under CC-BY-SA 2.5.