0

Hi Guys I am using the onbeforeunload method. But it fires when the user is actually navigating to any other page by clicking any link in the website/refreshing the Page which is not needed. I just need to find only when user closes the browser but not navigate or refresh the Page etc.. Isn't there . Thank you

     <script type="text/javascript">
          window.onbeforeunload = function (e) {
               var e = e || window.event;
               if (e) e.returnValue = 'Browser is being closed, is it okay?';//for IE & Firefox
               return 'Browser is being closed, is it okay?';// for Safari and Chrome
           };
        </script>
  • This might answer your question [How to capture the browser window close event?](https://stackoverflow.com/a/1632004/8795884). – tontonsevilla Aug 25 '20 at 07:59

1 Answers1

1

This would be impossible for the server to detect as the html is sent to the client and there isn't any active socket. A work around for this would be to make a socket with the user who is visiting your webpage, and when this socket is closed, you can assume the user has closed the page. But even then you can't detect a user closing a browser window.

johnsmh
  • 11
  • 1