Javascript 在卸载之前清除
我有一个与ajax一起提交的textarea,它使用onbeforeuload警告用户,如果用户试图关闭浏览器,他们没有提交textarea。问题是我需要在Javascript 在卸载之前清除,javascript,ajax,Javascript,Ajax,我有一个与ajax一起提交的textarea,它使用onbeforeuload警告用户,如果用户试图关闭浏览器,他们没有提交textarea。问题是我需要在ajaxsubmit的success中清除onbeforeunload,以便onbeforeunload知道用户已成功提交表单。是否有类似于clearTimeout或clearInterval的一行代码来清除onbeforeunload?我将显示当前的onbeforeunload代码,尽管我认为这并不重要。我正在寻找一个不同的函数来清除这个问
ajax
submit的success中清除onbeforeunload,以便onbeforeunload知道用户已成功提交表单。是否有类似于clearTimeout或clearInterval的一行代码来清除onbeforeunload?我将显示当前的onbeforeunload代码,尽管我认为这并不重要。我正在寻找一个不同的函数来清除这个问题。请不要。多谢各位
函数调用
unsavedChangesWarning();
Onbeforeunload函数
function unsavedChangesWarning(){
window.onbeforeunload = function(){
return 'If you close this page you will lose your stride update!';
};
}
window.onbeforeunload=null
将清除它。在尝试将用户重定向到任何位置之前,只需添加此选项。对于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.
}
};
根据“当此事件返回(或将returnValue属性设置为)null或undefined以外的值时,系统会提示用户确认页面卸载。”这至少在FF 61中是不正确的。我做了返回null和将e.returnValue设置为null的测试,对话框仍然显示。真正有效的方法是挂断听众的电话,比如Niet the Dark Absol或danielson317。