HashChangeEvent
The hashchange
event is fired when the fragment identifier of the URL has changed (the part of the URL that follows the # symbol, including the # symbol).
General info
- Specification
- HTML5
- Interface
- HashChangeEvent
- Bubbles
- Yes
- Cancelable
- No
- Target
- defaultView
- Default Action
- None
Properties
Property | Type | Description |
---|---|---|
target Read only |
EventTarget |
The browsing context (<code>window</code>). |
type Read only |
DOMString |
The type of event. |
bubbles Read only |
Boolean |
Whether the event normally bubbles or not |
cancelable Read only |
Boolean |
Whether the event is cancellable or not? |
oldURL Read only |
string | The previous URL from which the window was navigated. |
newURL Read only | string |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5.0 | 3.6 (1.9.2) Support for the oldURL /newURL attributes added in Firefox 6. |
8.0oldURL /newURL attributes are not supported. |
10.6 | 5.0 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 2.2 | 1.0 (1.9.2) | 9.0 | 11.0 | 5.0 |
There are several fallback scripts listed on this page. Basically those scripts check the location.hash at a regular interval. Here is a version that allows only one handler to be bound to the <code>window.onhashchange</code> property:
JavaScript
Copy Code
(function(window) { // exit if the browser implements that event if ( "onhashchange" in window.document.body ) { return; } var location = window.location, oldURL = location.href, oldHash = location.hash; // check the location hash on a 100ms interval setInterval(function() { var newURL = location.href, newHash = location.hash; // if the hash has changed and a handler has been bound... if ( newHash != oldHash && typeof window.onhashchange === "function" ) { // execute the handler window.onhashchange({ type: "hashchange", oldURL: oldURL, newURL: newURL }); oldURL = newURL; oldHash = newHash; } }, 100); })(window);
Syntax
JavaScript
Copy Code
window.onhashchange = funcRef;
or
JavaScript
Copy Code
<body onhashchange="funcRef();">
or
JavaScript
Copy Code
window.addEventListener("hashchange", funcRef, false);
Parameters
funcRef
- A reference to a function.
Example
JavaScript
Copy Code
if ("onhashchange" in window) { alert("The browser supports the hashchange event!"); } function locationHashChanged() { if (location.hash === "#somecoolfeature") { somecoolfeature(); } } window.onhashchange = locationHashChanged;
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. |
Related Events
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/hashchangeevent