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

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
hashchange eventChrome Full support 5Edge Full support 12Firefox Full support 3.6IE Full support 8Opera Full support 10.6Safari Full support 5WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 5Samsung Internet Android Full support 1.0

Legend

Full support  
Full support

参见