如何在IE8中捕获后退按钮点击事件

How to capture back button click event in IE8?

本文关键字:按钮 事件 IE8      更新时间:2023-09-26

当用户单击浏览器的后退按钮时,我试图执行一个函数。对于铬&firefox,我已经实现了html5的"popstate"事件。我无法在IE8中捕获后退按钮单击事件。在我的情况下,我需要捕获后退按钮单击事件,即使url/位置哈希没有改变。有人来帮忙吗??

  window.onbeforeunload = function (e) {
        var e = e || window.event;
        var msg = "Do you really want to leave this page?"
        // For IE and Firefox
        if (e) {
            e.returnValue = msg;
        }
        // For Safari / chrome
       return msg;
     };

嗯,从谷歌搜索中我发现了这个链接,似乎你可以区分刷新和点击返回:

window.onunload = function(e) {
// Firefox || IE
e = e || window.event;
var y = e.pageY || e.clientY;
if(y < 0)  alert("Window closed");
else alert("Window refreshed");
}

事件。pageY |事件。clientY:检测鼠标的位置。
窗口。Onunload:负责在页面关闭时执行一条指令。

我自己没有试过,但希望这能有所帮助。