如何知道在Firefox中点击刷新button或浏览器后退button

如何知道在Firefox中是否刷新button被点击或浏览器后退button被点击…两个事件onbeforeunload()方法是一个callback。 对于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.clientXevent.clientY总是0.是否有其他方法可以find它?

用于刷新事件

 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.'); }); 

使用“event.currentTarget.performance.navigation.type”来确定导航的types。 这是在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"); } } } 

对于jQuery中的后退button// jquery-latest.js

  jQuery(window).bind("unload", function() { // 

并在html5中有一个事件该事件被称为“popstate”

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

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

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

 var keyCode = evt.keyCode; if (keyCode==8) alert('you pressed backspace'); if(keyCode==116) alert('you pressed f5 to reload page')