0

I know window.onbeforeunload is the event that fire where tab or browser close. But I want to put some code in browser close event not tab close.

window.onbeforeunload = function (e) {
    $.ajax({
        url: "/User/UILayout/DisposeApplicationUser",
        type: 'post',
        dataType: 'json'
    })
};

I need to run ajax code just in close tab only. Is there any solution?

Sasan Karimi
  • 507
  • 3
  • 20
  • 1
    are you aware that ajax requests are not working in `onbeforeunload` handlers? – pwolaq Jan 24 '18 at 08:55
  • @pwolaq - It works but it fire in both event. I've searched a lot but didn't find out how can I manage between them. – Sasan Karimi Jan 24 '18 at 08:57
  • See this [question](https://stackoverflow.com/questions/34654113/difference-between-browser-tab-and-window). There seems to be no difference between a tab and the window of the browser other than the user experience, indeed. – vahdet Jan 24 '18 at 08:58
  • @vahdet - Seemingly there is no solution. – Sasan Karimi Jan 24 '18 at 09:11
  • Would like to understand the requirement or rather what you are trying to achieve by having this distinction? How does it matter whether tab is getting closed or browser? What different steps/actions are you trying to do with tab and browser? – Milan Chheda Jan 24 '18 at 09:15
  • See this https://stackoverflow.com/questions/16707249/detect-close-windows-event-by-jquery , we cannt detect the control over browser, only on the current page opened tab. Or else you need to check the active process running – VinuBibin Jan 24 '18 at 09:17
  • @milan-chheda - Imagine you've logged in your application I want when user close browser his session would be deleted but when he close its tab it wouldn't happen. – Sasan Karimi Jan 24 '18 at 09:19
  • @VinuBibin - Unfortunately this post has not gave me a hand – Sasan Karimi Jan 24 '18 at 09:23
  • @Mohamed SaȜed- He said _"I don't think you can natively detect a pure `close` in Javascript. Correct me if I'm wrong here."_ so finally there is no way to find out which event has fired! – Sasan Karimi Jan 27 '18 at 05:37

0 Answers0