0

In my application I have to forcefully log out user when they close tab or browser.I have logout function and I want to call it when user closes tab/browser.I have tried this piece of code but it didn't worked for me

<script type="text/javascript">
        $(function () {
            $(window).bind("beforeunload", function () {
                fnLogOut();
            })
        });
        function fnLogOut() { alert('browser closing'); }
 </script>

Any help?

Imran Qamer
  • 2,253
  • 3
  • 29
  • 52
dan
  • 78
  • 1
  • 12
  • 3
    Are you doing an ajax request inside `fnLogOut`? If so, you need to specify `async:false`, otherwise the browser will kill the thread before the ajax is sent. – bamtheboozle Oct 19 '17 at 06:40
  • browser tab is closed , where were you do your function ? – Jack jdeoel Oct 19 '17 at 06:42
  • You need to launch a popup which will do the logout and close itself. `onbeforeunload` is not guaranteed to keep the browser open till the logout is completed. – gurvinder372 Oct 19 '17 at 06:43
  • @gurvinder372 popup is not being launched on close of tab. i have tried above code – dan Oct 19 '17 at 06:53
  • Already we have post for this purpose: https://stackoverflow.com/questions/18783535/jquery-beforeunload-when-closing-not-leaving-the-page – Satyanarayana.Ruppa Oct 19 '17 at 06:53
  • Sometimes my browser crashes/freezes, and I kill the task in the taskmanager. What about that? `I have to forcefully log out user when they close tab or browser` Basically, why? Why can't you log them out in the backend if they are idle for a while? – Thomas Oct 19 '17 at 07:34

0 Answers0