如何在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.还有其他方法可以找到吗?
答案 0 :(得分:48)
用于刷新事件
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
和
$(window).unload(function() {
alert('Handler for .unload() called.');
});
答案 1 :(得分: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");
}
}
}
答案 2 :(得分:5)
用于后退按钮 在jquery // http://code.jquery.com/jquery-latest.js
jQuery(window).bind("unload", function() { //
并且在html5中有一个事件 该活动名为'popstate'
window.onpopstate = function(event) {
alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
并刷新请检查 Check if page gets reloaded or refreshed in Javascript
在Mozilla Client-x和client-y是文档区域内 https://developer.mozilla.org/en-US/docs/Web/API/event.clientX
答案 3 :(得分:-9)
var keyCode = evt.keyCode;
if (keyCode==8)
alert('you pressed backspace');
if(keyCode==116)
alert('you pressed f5 to reload page')