Событие hashchange
генерируется когда изменяется идентификатор фрагмента URL (т.е. часть URL следующая за символом #, включая сам символ #).
Общая информация
- Спецификация
- HTML5
- Интерфейс
- HashChangeEvent
- Bubbles
- Yes
- Cancelable
- No
- Target
- defaultView
- Действие по умолчанию
- None
Свойства
Property | Type | Description |
---|---|---|
target Только для чтения |
EventTarget |
The browsing context (window ). |
type Только для чтения |
DOMString |
The type of event. |
bubbles Только для чтения |
Boolean |
Whether the event normally bubbles or not. |
cancelable Только для чтения |
Boolean |
Whether the event is cancellable or not. |
oldURL Только для чтения |
String |
The previous URL from which the window was navigated. |
newURL Только для чтения | String |
Совместимость с браузерами
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
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 window.onhashchange
property:
(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);