0

I want to call a function when someone click on close button of browser. I gone through many posts and questions on Stack Overflow and Google groups. But couldn't find a way to accomplish this task. I want to send an ajax request when someone click on browser close button. Now I am using window.onbeforeunload JavaScript method but it is called each time when I click on a link or change web page.

window.onbeforeunload = function() {
alert("this method has been called")
return "Hey, you're leaving the site. Bye!";
};

But I want to call the method only on browser close button click. Is there any way by which I can catch browser close button clicked event? Help will be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sarfraz Ahmad
  • 1,419
  • 3
  • 15
  • 36
  • 1
    Click of link set a flag, in the onbeforeunload check the flag. Problem is refreshing the browser will trigger the message. Also all modern day browsers will not give you enough time to make an Ajax call to the server if that is what you were planning on. – epascarello Nov 25 '13 at 05:58
  • 1
    Also, if any web page gives me a pop-up asking if I'm sure I want to leave the site, and I'm not filling out a form or something where losing data is a possibility, I'm never going back to that page if I can help it. Just in case, a heads up about usability. – Amadan Nov 25 '13 at 05:58
  • generally not worth trying to make important data calls relying on browser closing.... think power fail! Not to mention cross browser handling is inconsistent – charlietfl Nov 25 '13 at 05:59
  • Then suggest me how should I do this. I want to save the time of user session in database.if user will close the browser without logout then using on browser close event i need to log him out and save the time in database, and i think it is only possible using an ajax call. Thanx all – Sarfraz Ahmad Nov 25 '13 at 06:10
  • 1
    As @charlietfl says, this is horribly unreliable. For example, what if the user simply powers off his computer? The only way to know for sure whether your user is on or not is to send Ajaxy pings from client side every, say, ten seconds or so, and update `last_seen` value or something similar on the server - i.e. detect presence (which is possible) instead of trying to get notification of absence (next to impossible with any degree of reliability). – Amadan Nov 25 '13 at 06:49
  • @Amadan well put regarding detecting presence not absence – charlietfl Nov 25 '13 at 06:56
  • yeah, you guys are right. The thing i am trying to do is highly unreliable. I have to choose an alternate that me be pins after sometimes or set the user logout time in database to a specific amount of time because if he don't send a request for 30 minutes his session will be expire. same amount of time can be added to last request time to update his logout time. may be I am wrong but i am thinking in this way right now. – Sarfraz Ahmad Nov 25 '13 at 07:02

2 Answers2

0
window.onclick = function() {
var win = window.open("/");
win.onload = function() {
    console.log("onload");
    win.onunload = function() {
        alert("Close onunload");
    }
}

}

Demo

Nandakumar
  • 1,071
  • 2
  • 11
  • 30
-1
$( window ).unload(function() {
  alert( "Handler for .unload() called." );
});

you can use this code

Shaik Rilwan
  • 365
  • 2
  • 8