-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
79 lines (54 loc) · 1.84 KB
/
index.md
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
---
title: "Window: hashchange event"
short-title: hashchange
slug: Web/API/Window/hashchange_event
page-type: web-api-event
browser-compat: api.Window.hashchange_event
---
{{APIRef}}
The **`hashchange`** event is fired when the fragment identifier of the URL has changed (the part of the URL beginning with and following the `#` symbol).
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
```js
addEventListener("hashchange", (event) => {});
onhashchange = (event) => {};
```
## Event type
A {{domxref("HashChangeEvent")}}. Inherits from {{domxref("Event")}}.
{{InheritanceDiagram("HashChangeEvent")}}
## Event properties
- {{domxref("HashChangeEvent.newURL")}} {{ReadOnlyInline}}
- : A string representing the new URL the window is navigating to.
- {{domxref("HashChangeEvent.oldURL")}} {{ReadOnlyInline}}
- : A string representing the previous URL from which the window was navigated.
## Event handler aliases
In addition to the `Window` interface, the event handler property `onhashchange` is also available on the following targets:
- {{domxref("HTMLBodyElement")}}
- {{domxref("HTMLFrameSetElement")}}
- {{domxref("SVGSVGElement")}}
## Examples
You can use the `hashchange` event in an {{domxref("EventTarget/addEventListener", "addEventListener")}} method:
```js
window.addEventListener(
"hashchange",
() => {
console.log("The hash has changed!");
},
false,
);
```
Or use the `onhashchange` event handler property:
```js
function locationHashChanged() {
if (location.hash === "#cool-feature") {
console.log("You're visiting a cool feature!");
}
}
window.onhashchange = locationHashChanged;
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{domxref("Window/popstate_event", "popstate")}} event