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 |