Michael
Michael

Reputation: 13616

How to catch event when web page closed only by button close

Any idea how to catch event when web page closed only by button close:

enter image description here

Upvotes: 0

Views: 1357

Answers (4)

Satinder singh
Satinder singh

Reputation: 10198

There is no specific event for capturing browser close event, but you can use unload event of the current page.

$( window ).unload(function() {
alert( "Bye now!" );
});

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


Updated:

$(window).on('beforeunload', function(){
      return 'Are you sure you want to leave?';
});

$(window).on('unload', function(){
      alert("Yes");
});

Upvotes: 1

Razvan Dumitru
Razvan Dumitru

Reputation: 12452

You cannot detect only that event.

The only way to detect window closing or tab closing ( user want to get out of your website ) is to use the onbeforeunload and onunload javascript events.

These events are triggered when you go back or when you click a link too, you can check that if you want. Also window.showModalDialog(), window.alert(), window.confirm(), and window.prompt() can misbehave during this process as you see here MozillaDeveloper.

Upvotes: 1

user5296864
user5296864

Reputation:

window.onbeforeunload = function() {
    return "Bye now!";
};

Just use simple working code

Upvotes: 1

Sunil Hari
Sunil Hari

Reputation: 1776

You can use the beforeunload event.

Try this sample code

 window.onbeforeunload = function (e)
        {

            e = e || window.event;
            var y = e.pageY || e.clientY;
            if (y < 0){
            return "Do You really Want to Close the window ?"
            }
            else {
            return "Refreshing this page can result in data loss."; 
            }

          }

Upvotes: 1

Related Questions