onhashchange
Event
Example
Execute JavaScript when the anchor part changes:
<body onhashchange="myFunction()">
Definition and Usage
The onhashchange event is triggered when the anchor part of the current URL (starting with '#') changes.
Example of an anchor part: Specify the current URL as
You can invoke the event using the following methods:
By modifying the anchor part through the Location Object's location.hash or location.href properties.
Navigating to the current page using different bookmarks (using the "back" or "forward" buttons)
Clicking on a link to jump to a bookmark anchor
Browser Support
The numbers in the table indicate the first browser version that supports the event.
Event | |||||
---|---|---|---|---|---|
onhashchange | 5.0 | 8.0 | 3.6 | 5.0 | 10.6 |
Syntax
In HTML:
In JavaScript:
In JavaScript, using the addEventListener() method:
Note: Internet Explorer 8 and earlier versions do not support the addEventListener() method.
Technical Details
Bubbles: | Yes |
---|---|
Cancelable: | No |
--- | --- |
Event Type: | Event |
--- | --- |
Supported HTML Tags: | <body> |
--- | --- |