我有一个用ajax提交的文本区,它使用onbeforeunload来警告用户,如果他们试图关闭浏览器,他们还没有提交文本区。问题是,我需要在ajax提交成功后以某种方式清除onbeforeunload,以便onbeforeunload知道用户已经成功提交了表单。有没有一种方法可以用一行代码来清除onbeforeunload,类似于clearTimeout或clearInterval?我将展示我目前拥有的onbeforeunload代码,尽管我认为b/c无关紧要,但我正在寻找一个不同的函数来清除它。请不要使用框架。谢谢。
函数调用
unsavedChangesWarning();Onbeforeunload函数
function unsavedChangesWarning(){
window.onbeforeunload = function(){
return 'If you close this page you will lose your stride update!';
};
}发布于 2012-12-31 03:24:02
window.onbeforeunload = null会清除它的。在尝试将用户重定向到任何位置之前,只需添加此代码。
发布于 2017-05-27 03:02:00
对于jQuery,这看起来像这样:
$(window).on('beforeunload', function (e)
{
return 'Please dont leave me';
});
$(window).off('beforeunload');实际上,如果用户会话过期,我将使用上面的解决方案来允许干净的刷新。但是,对于您的用例,我认为在卸载之前简单地对on进行条件设置更有意义。
$('input, textarea, select').change(function()
{
$(this).parents('form').addClass('dirty');
});
window.onbeforeunload = function()
{
if ($('form.dirty').length)
{
var confirmationMessage = 'You have edited but not saved a form on this page.';
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
}
};https://stackoverflow.com/questions/14093826
复制相似问题