hashchange
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
El evento hashchange
es ejecutado cuando el fragmento identificador de la URL ha cambiado (la parte de la URL que continúa despues del simbolo #, incluyendo el símbolo #).
Burbujas | Si |
Cancelable | No |
Objetivo | Window |
Interface | HashChangeEvent |
Acción predeterminada | Ninguna |
Propiedades
Property | Type | Description |
---|---|---|
target Read only |
EventTarget |
The browsing context (window ). |
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 |
The new URL to which the window is navigating. |
En esta página se enlistan algunos scripts de ejemplo. Básicamente estos scripts revisan el location.hash
en un intervalo regular. Aquí se muestra una versión que permite que solo un controlador sea ligado a la propiedad window.onhashchange
:
js
(function (window) {
// salir si el navegador implementa el evento
if ("onhashchange" in window) {
return;
}
var location = window.location,
oldURL = location.href,
oldHash = location.hash;
// revisa el hash cada 100ms
setInterval(function () {
var newURL = location.href,
newHash = location.hash;
// si el hash ha cambiado y un controlador ha sido ligado...
if (newHash != oldHash && typeof window.onhashchange === "function") {
// ejecuta el controlador
window.onhashchange({
type: "hashchange",
oldURL: oldURL,
newURL: newURL,
});
oldURL = newURL;
oldHash = newHash;
}
}, 100);
})(window);
Especificaciones
Specification |
---|
HTML Standard # event-hashchange |
HTML Standard # handler-window-onhashchange |
Compatibilidad con navegadores
BCD tables only load in the browser