Javascript - confirmation on page exit

I am trying to implement a basic popup that asks the user if they really want to leave a page similar to what will happen on this site if I try to close the window halfway by writing this message.

I understand that this is usually disapproving, but I have every reason to want to do this.

This works for me using the following code:

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

        e.cancelBubble = true;
        e.returnValue = 'Are you sure you want to leave?'; 

        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
    }

However, I would really like to show a message when they leave the page if they do not leave by clicking one of the two links.

(I just realized that it sounds like I can get them to click ads or something else!)

, . ( , , , - " , , ?).

, ?

.

.

+3
2

:

<head>
...
<script type="text/javascript">
var disabledConfirm_exit=false;
</script>
</head>

, ,

onclick="disabledConfirm_exit=true;"

_exit

function confirm_exit(e) {
    if(disabledConfirm_exit) return;
    if(!e) e = window.event;

    e.cancelBubble = true;
    e.returnValue = 'Are you sure you want to leave?'; 

    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
}
+3

window.unload javascript . : http://api.jquery.com/unload/

+2

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


All Articles