繁体   English   中英

检测用户是否要离开页面并使用ajax删除

[英]Detect if the user is leaving the page and delete with ajax

如果用户未完成我网页中的所有步骤,我希望删除已保存的数据。

这是我所做的:

window.onbeforeunload = confirmExit;

function confirmExit()
{
    if (!$('body').find('.wizard_completed').attr('class')) {
        if (confirm($('body').find('.exit_confirm').html())) {
            $.ajax({
                type: 'POST',
                data: {action: delete_event}
            });
        }
    }
}

问题是,如果用户单击“取消”,则即使不应该刷新页面,页面也仍然会刷新。 如果单击“取消”,如何避免刷新页面?

您将必须使用onunload事件进行AJAX调用,并在onbeforeunload显示以下消息:

window.onbeforeunload = confirmExit;
window.onunload = onunload;

function confirmExit()
{
    if (!$('body').find('.wizard_completed').attr('class')) {
        return $('body').find('.exit_confirm').html();
    }
}

function onunload(){
     $.ajax({
                type: 'POST',
                data: {action: delete_event}
            });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM