【问题标题】:How to capture back button click event in IE8?如何在 IE8 中捕获后退按钮单击事件?
【发布时间】:2014-04-28 13:08:32
【问题描述】:

当用户单击浏览器的后退按钮时,我正在尝试执行一个功能。对于 chrome & firefox,我通过 html5 "popstate" 事件实现了它。我无法在 IE8 中捕获后退按钮单击事件。就我而言,即使 url/位置哈希没有改变,我也需要捕获后退按钮单击事件。有人帮忙吗??

【问题讨论】:

标签: javascript jquery html events internet-explorer-8


【解决方案1】:

嗯,从谷歌搜索我找到了这个link,看来你可以区分刷新和点击返回:

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");

}

event.pageY | event.clientY:检测鼠标位置。
window.onunload:负责在页面关闭时执行指令。

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

【讨论】:

    【解决方案2】:
      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;
         };
    

    【讨论】:

    • 感谢您的宝贵时间.. 我想,这也适用于刷新按钮.. 我的要求是只捕获后退按钮单击事件..
    猜你喜欢
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    相关资源
    最近更新 更多