1let disableConfirmation = false;
2window.addEventListener('beforeunload', event => {
3 const confirmationText = 'Are you sure?';
4 if (!disableConfirmation) {
5 event.returnValue = confirmationText; // Gecko, Trident, Chrome 34+
6 return confirmationText; // Gecko, WebKit, Chrome <34
7 } else {
8 // Set flag back to false, just in case
9 // user stops loading page after clicking a link.
10 disableConfirmation = false;
11 }
12});
13
14After Confimation if you want to send any ajax request and you have used jquery
15 $(window).on('unload', function() {
16 // async: false will make the AJAX synchronous in case you're using jQuery
17 axios
18 .get('ajax_url')
19 .then(response => {});
20 });
21
22And if no jquery is used you can use navigator, it require navigator library
23
24navigator.sendBeacon(url, data);
25
26