问题描述
我有这个功能
$ p $ window.onbeforeunload = function(){
return你确定吗? ;
};
我有一个按钮,当用户按下它时,窗口刷新以更新屏幕值并执行一些操作数据库调用。我在函数的开头添加了 window.onbeforeunload = null;
来禁用onbeforeunload,但它一直被调用。
有没有办法在我的按钮onclick函数中禁用onbeforeunload函数,或者让window.onbeforeunload只在关闭窗口中调用而不是刷新?
以下代码应该可以工作(应该保留在脚本标签之间的head标签内)
window.onload = function(){
window.onbeforeunload = function(){
returnAre you sure?;
}
document.getElementById('refresh')。onclick = function(){
window.onbeforeunload = null;
window.location.reload(); //替换为你的代码
code $
$ b $
I have this function
window.onbeforeunload = function () {
return "Are you sure?";
};
and I have a button that when the user presses it the window refreshes to update screen values and do some database calls. I added window.onbeforeunload=null;
in the beginning of the function to disable onbeforeunload but it kept being called.
Is there a way to either disable the onbeforeunload function in my button onclick function or make the window.onbeforeunload get called only on close window not on refresh?
解决方案 Following code should work (should be kept inside head tag between script tags)
window.onload=function(){
window.onbeforeunload = function(){
return "Are you sure?";
}
document.getElementById('refresh').onclick=function(){
window.onbeforeunload = null;
window.location.reload(); // replace with your code
}
}
这篇关于window.onbeforeunload仅在关闭窗口而不是刷新时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!