Window: hashchange event

当URL的片段标识符更改时,将触发hashchange事件 (跟在#符号后面的URL部分,包括#符号)

Bubbles Yes
Cancelable No
Interface HashChangeEvent
Event handler onhashchange

示例

你可以在 addEventListener 方法中使用 hashchange 事件:

window.addEventListener('hashchange', function() {
  console.log('The hash has changed!')
}, false);

或使用 onhashchange 事件处理程序属性:

function locationHashChanged() {
  if (location.hash === '#cool-feature') {
    console.log("You're visiting a cool feature!");
  }
}

window.onhashchange = locationHashChanged;

规范

规范 状态 注释
HTML Living Standard
hashchange
Living Standard Initial definition

浏览器兼容性

BCD tables only load in the browser

参见