onhashchange event
Example
Execute JavaScript when the anchor part changes:
Try it »
Definition and Usage
The onhashchange event is triggered when the anchor part of the current URL (starting with '#') changes.
Example of an anchor part: suppose the current URL is
http://www.example.com/test.htm#part2 - the anchor part in this URL is #part2.
You can invoke the event in the following ways:
- By settingLocation object 's location.hash or location.href property to modify the anchor part.
- Navigate to the current page using different bookmarks (using the "Back" or "Forward" button)
- Click a link to jump to a bookmark anchor
Browser support
The numbers in the table indicate the version number of the first browser to support the event.
| Events | |||||
|---|---|---|---|---|---|
| onhashchange | 5.0 | 8.0 | 3.6 | 5.0 | 10.6 |
Syntax
In HTML:
<
element
onhashchange="
myScript
">Try it
In JavaScript:
object
.onhashchange=function(){
myScript
};
Try it
In JavaScript, use the addEventListener() method:
object
.addEventListener("hashchange",
myScript
);
Try it
Note: Internet Explorer 8 and earlier IE versions do not support addEventListener() method.
Technical Details
| Does it support bubbling: | Yes |
|---|---|
| Is it cancelable: | No |
| Event type: | Event |
| Supported HTML tags: |
Event Object