我用jQuery为浏览器的后退键绑定事件,即当用户点击了后退键时,处理我的事件响应。在网上查看了相关资料,发现下面这段代码在FireFox和Chrome浏览器都有效,唯独在IE浏览器失效。
$(function ($) {
if (window.history && window.history.pushState) {
$(window).on('popstate', function () {
var hashLocation = location.hash;
var hashSplit = hashLocation.split("#!/");
var hashName = hashSplit[1];
if (hashName !== '') {
var hash = window.location.hash;
if (hash === '') {
alert("Back button isn't supported. You are leaving this application on next clicking the back button");
}
}
});
window.history.pushState('forward', null, './#forward');
}
});
所以如何在IE浏览器中监听后退键呢?请了解过相关内容的告知下,谢谢。
IE 在
hash
改变的时候不会触发popstate
事件。(参考popstate issue)可以尝试监听 onhashchange 事件, 然后在事件处理函数你们判断是前进还是后退。