I have used the (window.onunload) and (window.onbeforeunload) event for capturing browser close event to kill session. but it doesn't work for me in chrome. Is there any way to do?
Asked
Active
Viewed 516 times
2
-
thanks for reply but idont know about navigator.sendBeacon. – lalit Jan 09 '19 at 05:10
1 Answers
4
Normal async XMLHttpRequest
(including fetch
) tend to be cancelled as the page closes. The usual workaround has been to make synchronous requests.
However, there's a new way to do it: navigator.sendBeacon
. It was designed with pretty much that exact purpose in mind - it will queue a request to be executed even after the page is closed.
window.addEventListener("unload", function() {
navigator.sendBeacon("/logout");
});
It still doesn't work on all browsers, so if you care about the lag-behinders you might want to test if it is available, and use the above-mentioned workaround if not.

Amadan
- 191,408
- 23
- 240
- 301