We want to help developers like you. Tell us about how you work: http://qsurvey.mozilla.com/s3/Developer-Audience-Survey-V2/?s=mdn

window.onhashchange

hashchange事件在当前页面URL中的hash值发生改变时触发 (查看location.hash).

语法

window.onhashchange = funcRef;

或者

<body onhashchange="funcRef();">

再或

window.addEventListener("hashchange", funcRef, false);

参数

  • funcRef 是个函数引用

例子

if ("onhashchange" in window) {
    alert("该浏览器支持hashchange事件!");
}

function locationHashChanged() {
    if (location.hash === "#somecoolfeature") {
        somecoolfeature();
    }
}

window.onhashchange = locationHashChanged;

hashchange事件

hashchange 事件对象有下面两个属性

属性 类型 描述
newURL DOMString 当前页面新的URL
oldURL DOMString 当前页面旧的URL

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 5.0 3.6 (1.9.2) 8.0 10.6 5.0
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.2 1.0 (1.9.2) 9.0 11.0 5.0

规范

相关链接

Manipulating the browser history

history.pushState() 

history.replaceState()

window.onpopstate

文档标签和贡献者

 此页面的贡献者: teoli, khalid32, ziyunfei
 最后编辑者: khalid32,