问题描述
我正在使用onbeforeunload
事件在关闭页面期间执行操作.
我不希望事件在Refresh
/的情况下发生.
有没有办法做到这一点?
不幸的是,onbeforeunload
事件在浏览器中侦听页面状态.转到另一个页面以及刷新将更改页面状态,这意味着onbeforeunload
仍将被触发.
所以我认为不可能只捕捉刷新.
但是,如果您通过JavaScript监听并阻止Keypress
,则可以实现.
使用jQuery .keydown(),您可以检测到以下键代码:
对于
$(document).keydown(function (e) {
if (e.keyCode == 65 && e.ctrlKey) {
e.preventDefault();
}
});
对于
$(document).keydown(function (e) {
if (e.which || e.keyCode) == 116) {
e.preventDefault();
}
});
I'm using onbeforeunload
event to perform operations during the closing page.
I do not want the event to happen in the case of Refresh
/ .
Is there a way or other event to do this?
Unfortunately onbeforeunload
event listens the page state in the browser. Going to another page as well as refreshing will change the page state, meaning onbeforeunload
will be triggered anyway.
So I think it is not possible to catch only refresh.
But, if you'll listen and prevent Keypress
via JavaScript, then it can be achieved.
Refresh can be done via and keys, so your goal will be to prevent these actions.
using jQuery .keydown() you can detect these keycodes:
For
$(document).keydown(function (e) {
if (e.keyCode == 65 && e.ctrlKey) {
e.preventDefault();
}
});
For
$(document).keydown(function (e) {
if (e.which || e.keyCode) == 116) {
e.preventDefault();
}
});
这篇关于防止在刷新/F5中发生OnBeforeUnload()事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!