onhashchangeEvent

事件对象参考手册Event Object

Examples

Execute JavaScript when the anchor part changes:

<body onhashchange="myFunction()">

Try it yourself »


Definition and Usage

The onhashchange event is triggered when the anchor part of the current URL (starting with the '#' sign) changes.

Example of anchor part: specify the current URL as
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 setting theLocation objectof location.hashorlocation.hrefproperty to modify the anchor part.
  • Using different bookmarks to navigate to the current page (using the "Back" or "Forward" buttons)
  • Clicking a link to jump to a bookmark anchor

Browser Support

The numbers in the table indicate the version number of the first browser that supports this event.

Event
onhashchange 5.0  8.0 3.6 5.0 10.6


Syntax

In HTML:

<element onhashchange="myScript">Try it yourself

In JavaScript:

object.onhashchange=function(){myScript};Try it yourself

In JavaScript, using the addEventListener() method:

object.addEventListener("hashchange", myScript);Try it yourself

Note:Internet Explorer 8 and earlier IE versions do not supportaddEventListener()method.


Technical Details

Does it support bubbling: Yes
Is it cancelable: No
Event type: Event
Supported HTML tags: <body>


事件对象参考手册Event Object

Other Extensions