aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/api/window/hashchange_event/index.html
blob: 653f922c066f8bb90063d1706251a56ebbfd99a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
title: hashchange
slug: Web/API/Window/hashchange_event
tags:
  - Referencia
  - URL
  - Web
translation_of: Web/API/Window/hashchange_event
---
<p>El evento <code>hashchange</code> 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 #).</p>

<table class="properties">
 <tbody>
  <tr>
   <td>Burbujas</td>
   <td>Si</td>
  </tr>
  <tr>
   <td>Cancelable</td>
   <td>No</td>
  </tr>
  <tr>
   <td>Objetivo</td>
   <td>{{domxref("Window")}}</td>
  </tr>
  <tr>
   <td>Interface</td>
   <td>{{domxref("HashChangeEvent")}}</td>
  </tr>
  <tr>
   <td>Acción predeterminada</td>
   <td>Ninguna</td>
  </tr>
 </tbody>
</table>

<h2 id="Propiedades">Propiedades</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Property</th>
   <th scope="col">Type</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>target</code> {{readonlyInline}}</td>
   <td>{{domxref("EventTarget")}}</td>
   <td>The browsing context (<code>window</code>).</td>
  </tr>
  <tr>
   <td><code>type</code> {{readonlyInline}}</td>
   <td>{{domxref("DOMString")}}</td>
   <td>The type of event.</td>
  </tr>
  <tr>
   <td><code>bubbles</code> {{readonlyInline}}</td>
   <td>{{jsxref("Boolean")}}</td>
   <td>Whether the event normally bubbles or not.</td>
  </tr>
  <tr>
   <td><code>cancelable</code> {{readonlyInline}}</td>
   <td>{{jsxref("Boolean")}}</td>
   <td>Whether the event is cancellable or not.</td>
  </tr>
  <tr>
   <td><code>oldURL</code> {{readonlyInline}}</td>
   <td>{{jsxref("String")}}</td>
   <td>The previous URL from which the window was navigated.</td>
  </tr>
  <tr>
   <td>newURL {{readonlyInline}}</td>
   <td>{{jsxref("String")}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<p>En <a href="https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills">esta página</a> se enlistan algunos scripts de ejemplo. Básicamente estos scripts revisan el <code>location.hash</code> en un intervalo regular. Aquí se muestra una versión que permite que solo un controlador sea ligado a la propiedad <code>window.onhashchange</code>:</p>

<pre class="brush: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 &amp;&amp; typeof window.onhashchange === "function") {
      // ejecuta el controlador
      window.onhashchange({
        type: "hashchange",
        oldURL: oldURL,
        newURL: newURL
      });

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

})(window);
</pre>

<h2 id="Especificaciones">Especificaciones</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Especificacion</th>
   <th scope="col">Estado</th>
   <th scope="col">Comentario</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'indices.html#event-hashchange', 'hashchange')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>Definición inicial</td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilidad_con_navegadores">Compatibilidad con navegadores</h2>

{{Compat("api.Window.hashchange_event")}}

<h2 id="Ver_también">Ver también</h2>

<ul>
 <li><a href="/en-US/docs/Mozilla_event_reference/popstate"><code>popstate</code></a></li>
 <li><a href="/en-US/docs/Web/API/WindowEventHandlers/onhashchange">WindowEventHandlers.onhashchange</a></li>
</ul>