Ajax and onbeforeunload

I want to inform my server that the user has closed the tab / browser window with our application.

I found out that starting an ajax call with async:false tied to window.beforeunload evet will work and that will happen. Sorting...

 window.onbeforeunload = beforeunload; function beforeunload() { setSendingPowerStatus(false, true); } 

Thus, in chrome, the user closes the window and the function setSendingPowerStatus(false, true); , which in its implementation makes a synchronous call, and when the browser completes the tab. In FF, on the other hand, the user is prompted with this warning box β€œAre U sure you want to leave the page?”.

My question is how to make ajax call in onbeforeunload without showing any dialogue ??? I want to say that I want to make an ajax call before the window is closed without bothering the user ...

I also tried to return false also from my handler function, but without luck too ...

+4
source share
1 answer

I suggest you try again (setting returnValue to true and returning true):

 function beforeunload(event) { setSendingPowerStatus(false, true); event.returnValue=true; return true; } window.onbeforeunload = beforeunload; 
0
source

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


All Articles