如何在 Firefox 中知道是否单击了刷新按钮或单击了浏览器后退按钮?对于这两个事件,
onbeforeunload()
方法都是回调。对于 Internet Explorer,我的处理方式如下:
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 {
// I 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.';
};
参见 窗口:卸载事件之前。
还有
$(window).unload(function() {
alert('Handler for .unload() called.');
});
使用“event.currentTarget.performance.navigation.type”来确定导航类型。
这适用于 Internet Explorer、Firefox 和 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 中:
// 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));
};
要刷新,请检查 检查页面是否在 JavaScript 中重新加载或刷新。
在 Mozilla 中,Client-x 和 client-y 位于文档区域内。请参阅 MouseEvent.clientX。
var keyCode = evt.keyCode;
if (keyCode == 8)
alert('you pressed backspace');
if(keyCode == 116)
alert('you pressed F5 to reload the page')