--- title: hashchange slug: Web/API/Window/hashchange_event tags: - Reference - Web translation_of: Web/API/Window/hashchange_event ---

L'événement hashchange est déclenché lorsque l'identificateur de fragment de l'URL a changé (la partie de l'URL qui suit le symbole #, y compris le symbole # lui-même).

Bulles Oui
Annulable Non
Objets cibles {{domxref("Window")}}
Interface {{domxref("HashChangeEvent")}}
Action par défaut Aucune

Propriétés

Propriété Type Description
target {{readonlyInline}} {{domxref("EventTarget")}} The browsing context (window).
type {{readonlyInline}} {{domxref("DOMString")}} Type de l'évènement
bubbles {{readonlyInline}} {{jsxref("Boolean")}} Whether the event normally bubbles or not.
cancelable {{readonlyInline}} {{jsxref("Boolean")}} Whether the event is cancellable or not.
oldURL {{readonlyInline}} {{jsxref("String")}} The previous URL from which the window was navigated.
newURL {{readonlyInline}} {{jsxref("String")}}

Il existe plusieurs scripts de secours listés sur cette page. Fondamentalement, ces scripts vérifient le location.hash à intervalles réguliers. Voici une version qui n'autorise qu'un seul gestionnaire à être lié à la propriété window.onhashchange:

;(function(window) {

  // Sortir si le navigateur implémente cet événement
  if ("onhashchange" in window) { return; }

  var location = window.location,
    oldURL = location.href,
    oldHash = location.hash;

  // Vérifie la hash de la barre d'adresse toutes les 100ms
  setInterval(function() {
    var newURL = location.href,
      newHash = location.hash;

    // Si le hash a été changé et qu'un gestionnaire a été lié...
    if (newHash != oldHash && typeof window.onhashchange === "function") {
      // exécute le gestionnaire
      window.onhashchange({
        type: "hashchange",
        oldURL: oldURL,
        newURL: newURL
      });

      oldURL = newURL;
      oldHash = newHash;
    }
  }, 100);

})(window);

Spécifications

Spécification Status Commentaire
{{SpecName('HTML WHATWG', 'indices.html#event-hashchange', 'hashchange')}} {{Spec2('HTML WHATWG')}} Définition initiale

Compatibilité des navigateurs

{{CompatibilityTable}}

Fonctionnalité Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Support basique 5.0 {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.9.2")}}[1] 8.0[2] 10.6 5.0
Fonctionnalité Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Support basique 2.2 {{CompatVersionUnknown}} {{CompatGeckoMobile("1.9.2")}} 9.0 11.0 5.0

[1] Support pour les attributs oldURL/newURL ajouté à  Firefox 6.

[2] Les attributs oldURL/newURL ne sont pas supportés.

Voir également