How to determine onbeforeunload was caused by clicking the close button

How to determine if onbeforeunload was triggered by a button to close or refresh the page, or what event triggered onbeforeunload in general?

Here is a snippet:

window.onbeforeunload = function( e ){
    if( !e ) e = window.event;

    // insert code to determine which is the source of the event
}

Please help me.

+3
source share
4 answers

, , , , , . . , . , "" ( "X" ).

$(window).on('mouseover', (function () {
    window.onbeforeunload = null;
}));


$(window).on('mouseout', (function () {
    window.onbeforeunload = ConfirmLeave;
}));

function ConfirmLeave() {
    return "";
}

ConfirmLeave , , ,

, :)

+4

, , onbeforeunload. , , - .

+2

, e.clientY . , .

window.onbeforeunload = function() {
   var e = window.event;
   alert(e.clientX + " / " + e.clientY);
}
0

- , .

, . . (Untested).

var target = $( e.target );
if(!target.is("a, :button, :submit, :input, .btn, .bulkFormButton")){
//Your code for browser events)
}
   $("form").submit(function () {
             //Your code for browser events)
   });

This worked for me, but there are still some events that are not being processed. I am looking for them. If anyone has ideas about them, please share.

0
source

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


All Articles