2014-07-21 98 views
0

我需要關閉用戶會話與AJAX,當這關閉我的應用程序的導航或選項卡。爲此,我使用beforeunload事件,問題是當我用F5(cmr + r,ctrl + r)重新加載頁面時,用這個nav打開警告診斷,我不需要這個。事件之前卸載

$(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" 
    }); 
    } 
}); 

等待回信,謝謝。

+1

可能的重複:http://stackoverflow.com/questions/5606331/how-to-prevent-calling-onbeforeunload-when-page-refresh – Kaiido

回答

0

無法區分關閉標籤和標籤卸載/重新加載。相反您的舊方法,您可以設置間隔5分鐘或一些有意義的時間段,並向服務器發送有關會話的通知。