suppress confirmation dialog while using onbeforeunload

前端 未结 2 957
醉话见心
醉话见心 2021-01-12 16:47

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

When I am using onbeforeunload, it shows the confirmation dialog on closing the t

相关标签:
2条回答
  • 2021-01-12 17:18

    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;
    }
    
    0 讨论(0)
  • 2021-01-12 17:31

    When the user closes the page the document is dead, the scripts are dead. You're expecting the script to run after they close. Now if the tab doesn't close it feels like it's frozen waiting for your function to finish. Meaning it's not possible. I believe that's what you want? The clean up request won't send because the document is closed.

    0 讨论(0)
提交回复
热议问题