如何在计时器超时时禁用 window.onbeforeunload 警报
how to diable window.onbeforeunload alert when timer is timedout
我已经使用 window.onbeforeunload() 在用户导航离开页面时显示警告,并且效果很好,
jQuery(window).bind('beforeunload', function (e) {
var message = "Attention:";
e.returnValue = message;
return message;
});
在我的项目的一个视图中,我使用了一个 timer.The 问题是,当计时器超时时,会显示一个警报,然后是 window.onbeforeunload() 警报.
我只想在定时器超时时显示定时器警报,而不是两者都显示。
我怎样才能做到这一点?
正如 Shaunak 评论的那样,您应该在取消绑定 "hardcoded" beforeunload
函数定义之前:
jQuery(window).unbind();
然后重新定义它:
jQuery(window).bind('beforeunload', function (e) {
var message = "Attention:";
e.returnValue = message;
return message;
});
对于unbind
一个事件,您需要引用原始处理程序。
请看下面
var func = function(e) {
var message = "Attention:";
e.returnValue = message;
return message;
};
jQuery(window).bind('beforeunload', func);
// Unbind 'beforeunload' after 10 seconds
setTimeout(function() {
jQuery(window).unbind('beforeunload', func);
alert('beforeunload unbound');
}, 10000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我已经使用 window.onbeforeunload() 在用户导航离开页面时显示警告,并且效果很好,
jQuery(window).bind('beforeunload', function (e) {
var message = "Attention:";
e.returnValue = message;
return message;
});
在我的项目的一个视图中,我使用了一个 timer.The 问题是,当计时器超时时,会显示一个警报,然后是 window.onbeforeunload() 警报.
我只想在定时器超时时显示定时器警报,而不是两者都显示。
我怎样才能做到这一点?
正如 Shaunak 评论的那样,您应该在取消绑定 "hardcoded" beforeunload
函数定义之前:
jQuery(window).unbind();
然后重新定义它:
jQuery(window).bind('beforeunload', function (e) {
var message = "Attention:";
e.returnValue = message;
return message;
});
对于unbind
一个事件,您需要引用原始处理程序。
请看下面
var func = function(e) {
var message = "Attention:";
e.returnValue = message;
return message;
};
jQuery(window).bind('beforeunload', func);
// Unbind 'beforeunload' after 10 seconds
setTimeout(function() {
jQuery(window).unbind('beforeunload', func);
alert('beforeunload unbound');
}, 10000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>