0

i need close the user session with ajax, when this close the nav or tab of my app. For this i'm using the event beforeunload, the issue is when i reload the page with F5(cmr+r, ctrl+r) with this the nav open the warning diag and i don't need this.

$(window).bind('beforeunload',function (e) {
   var message = 'Are you sure you want to leave?';
   return message;
 }

$(window).bind('unload', function () {
  var dataStatus = $('button[name="status"]').attr('data-status');
  if (dataStatus == 'online') {
    var req = $.ajax({
      url: 'mi-url',
      type: 'POST',
      async : true,
      data: {data: JSON.stringify({user_id: <?php echo $user->getId() ?>})},
      dataType: "JSON"
    });
  }
});

Waiting for replys, thanks.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Marcelo
  • 115
  • 5
  • 1
    Possible duplicate : http://stackoverflow.com/questions/5606331/how-to-prevent-calling-onbeforeunload-when-page-refresh – Kaiido Jul 21 '14 at 19:41

1 Answers1

0

You can't differentiate close tab and tab unload/reload. Instead your old approach, you can set interval 5 minutes or some meaningful time period, and send a notification to the server about session.

HILARUDEEN S ALLAUDEEN
  • 1,722
  • 1
  • 18
  • 33