WindowEventHandlers.onhashchange

O evento hashchange é disparado quando a hash da window muda. (ver location.hash).

Sintaxe

js
window.onhashchange = funcRef;

ou:

html
<body onhashchange="funcRef();"></body>

ou:

js
window.addEventListener("hashchange", funcRef, false);

Parâmetros

funcRef

Referência a uma função

Exemplo

js
if ("onhashchange" in window) {
  alert("O browser têm suporte ao evento hashchange!");
}

function locationHashChanged() {
  if (location.hash === "#algointeressante") {
    algoInteressante();
  }
}

window.onhashchange = locationHashChanged;

O evento hashchange

O evento hashchange disparado possui os seguintes campos

Campo Tipo Descrição
newURL DOMString A nova URL para a qual a janela está navegando.
oldURL DOMString A antiga URL da qual a janela veio.

Especificações

Specification
HTML Standard
# event-hashchange
HTML Standard
# handler-window-onhashchange

Compatibilidade com navegadores

BCD tables only load in the browser