Is there a way to detect page closure by javascript?

I watched several threads here, mostly used this: window.onbeforeunload = function (e) {var e = e || window.event;

// IE and Firefox if (e) {e.returnValue = 'Are you sure?'; }

// For Safari return 'Are you sure?'; };

but it looks like this will confuse the situation with the refresh of the page and the closing page, is there any way to distinguish between these two situations?

+3
source share
2 answers
function OnUnLoad()
{
    if (window.event.clientY < 0 && window.event.clientX < 0)
    {
        //cleanup code goes here...
    }       
}

This seems to work for me in all browsers. But closing tabs seems to be causing problems. There is really no “documented" way to do this.

+3
source

Javascript. GMail AJAX , . , , "OnBeforeUnload", - . JS , OnBeforeUnload script, , , , .

, .

+2

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


All Articles