编程语言
首页 > 编程语言> > javascript – 如何知道Firefox中是否单击了刷新按钮或浏览器后退按钮

javascript – 如何知道Firefox中是否单击了刷新按钮或浏览器后退按钮

作者:互联网

参见英文答案 > detect back button click in browser                                     6个
如何在Firefox中了解是单击了刷新按钮还是单击了浏览器后退按钮…对于这两个事件onbeforeunload()方法都是回调.对于IE我正在这样处理:

function CallbackFunction(event) {
    if (window.event) {
        if (window.event.clientX < 40 && window.event.clientY < 0) {
            alert("back button is clicked");
        }else{
            alert("refresh button is clicked");
        }
    }else{
        // want some condition here so that I can differentiate between
        // whether refresh button is clicked or back button is clicked.
    }
}

<body onbeforeunload="CallbackFunction();"> 

但在Firefox中,event.clientX和event.clientY始终为0.还有其他方法可以找到它吗?

解决方法:

用于刷新事件

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};

https://developer.mozilla.org/en-US/docs/Web/API/window.onbeforeunload?redirectlocale=en-US&redirectslug=DOM%2Fwindow.onbeforeunload

$(window).unload(function() {
      alert('Handler for .unload() called.');
});

标签:javascript,browser,cross-browser,dom-events
来源: https://codeday.me/bug/20190916/1808584.html