Nossos voluntários ainda não traduziram este artigo para o Português (do Brasil). Junte-se a nós e ajude-nos a fazer o trabalho!
Você também pode ler o artigo em English (US).

The HashChangeEvent 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

HashChangeEvent.newURL Read only
The new URL to which the window is navigating.
HashChangeEvent.oldURL Read only
The previous URL from which the window was navigated.

Syntax

window.onhashchange = funcRef;

or

<body onhashchange="funcRef();">

or

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

Parameters

funcRef
A reference to a function.

Example

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

window.addEventListener("hashchange", locationHashChanged);

Polyfill

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:

(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);

Specifications

Specification Status Comment
HTML Living Standard
The definition of 'HashChangeEvent' in that specification.
Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 5Edge Full support YesFirefox Full support 3.6IE Full support 8Opera Full support 10.6Safari Full support 5WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.0Safari iOS Full support 5Samsung Internet Android ?
oldURLChrome Full support YesEdge ? Firefox Full support 6IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
newURLChrome Full support YesEdge ? Firefox Full support 6IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

Etiquetas do documento e colaboradores

Última atualização por: mfluehr,