使用onbeforeunload时取消确认对话框 [英] suppress confirmation dialog while using onbeforeunload

查看:543
本文介绍了使用onbeforeunload时取消确认对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用onbeforeunload事件发送ajax请求来执行一些清理任务.

I am using onbeforeunload event to send ajax request to perform some clean up task.

当我使用onbeforeunload时,它会在关闭选项卡时显示确认对话框. 我想要的是不显示任何确认对话框,而只是发送清理请求.以下是我正在使用的脚本.

When I am using onbeforeunload, it shows the confirmation dialog on closing the tab. What I want is not to show any confirmation dialog and just send the clean up request. Following is the script I am using.

    window.onbeforeunload = unloadFunction;
    function unloadFunction() {
        var test_id = $('#test_id').val();

        jQuery.ajax({
            url: "/test/cleanup/" + test_id,
            cache: false
        }).done(function () {
            return false;
        });
        return false;
    }

有什么办法可以禁止确认对话框?

Is there any way I can suppress the confirmation dialog?

根据一些建议,我试图将return语句更改为return ;而不是return false;.但这也不起作用.

As per some suggestions, I have tried to change return statement to return ; instead of return false; . But this is also not working.

推荐答案

根据我对原始帖子的评论,答案是通过在Ajax调用设置中添加async: false来使调用同步,更改最后返回false到return null;,并删除完成的功能:

As per my comments on the original post, the answer is to make the call synchronous by adding async: false in the Ajax call settings, change the last return false to return null;, and remove the done function:

window.onbeforeunload = unloadFunction;
function unloadFunction() {
    var test_id = $('#test_id').val();

    jQuery.ajax({
        url: "/test/cleanup/" + test_id,
        cache: false,
        async: false
    });

    return null;
}

这篇关于使用onbeforeunload时取消确认对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆