Suppress confirmation dialog when using onbeforeunload

I use onbeforeunload event to send an ajax request to do some cleanup task.

When I use onbeforeunload, it shows a confirmation dialog when closing the tab. I want to not show a confirmation dialog and just send a cleanup request. Below is the script.

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 to suppress the confirmation dialog?

According to some suggestions, I tried changing the return statement to return ; instead of return false; . But this also does not work.

+5
source share
2 answers

According to my comments on the original post, the answer is to make a synchronous call by adding async: false to the Ajax call settings, change the last value of return false to return null; and delete the executed 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; } 
+2
source

When the user closes the page, the document is dead, the scripts are dead. You expect the script to be run after they are closed. Now, if the tab does not close, it seems that it has frozen, waiting for the completion of your function. It's impossible. I believe what you want? A cleanup request will not be sent because the document is closed.

+1
source

Source: https://habr.com/ru/post/1209295/


All Articles