简体   繁体   English

卸载前事件

[英]Event beforeunload

i need close the user session with ajax, when this close the nav or tab of my app. 当关闭我的应用程序的导航或选项卡时,我需要使用ajax关闭用户会话。 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. 为此,我正在使用事件beforeunload,问题是当我用F5(cmr + r,ctrl + r)重新加载页面时,此导航打开了警告对话框,我不需要这个。

$(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. 等待回复,谢谢。

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. 您可以设置5分钟间隔或某个有意义的时间间隔,而不是以前的方法,然后向服务器发送有关会话的通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM