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

Cod*_*ing 49 javascript browser cross-browser dom-events

如何知道在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();"> 
Run Code Online (Sandbox Code Playgroud)

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

Er.*_*.KT 54

用于刷新事件

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};
Run Code Online (Sandbox Code Playgroud)

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.');
});
Run Code Online (Sandbox Code Playgroud)

  • 这如何区分重载和后退按钮? (34认同)
  • 不回答问题:-\ (4认同)

小智 11

使用'event.currentTarget.performance.navigation.type'确定导航类型.这适用于IE,FF和Chrome.

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{
        if (event.currentTarget.performance.navigation.type == 2) {
            alert("back button is clicked");
        }
        if (event.currentTarget.performance.navigation.type == 1) {
            alert("refresh button is clicked");
        }           
    }
}
Run Code Online (Sandbox Code Playgroud)

  • event.currentTarget.performance.navigation.type对我不起作用,即使event.currentTarget.performance显示未定义.你会更多地了解这一点,更多的代码或脚本. (2认同)

dev*_*rCK 5

对于jquery中的后退按钮// http://code.jquery.com/jquery-latest.js

 jQuery(window).bind("unload", function() { //
Run Code Online (Sandbox Code Playgroud)

在html5中有一个事件 该事件被称为'popstate'

window.onpopstate = function(event) {
alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
Run Code Online (Sandbox Code Playgroud)

并刷新请检查 是否在Javascript中重新加载或刷新页面

在Mozilla Client-x和client-y位于文档区域 https://developer.mozilla.org/en-US/docs/Web/API/event.clientX