-1

I'm trying to force the user to LogOut from the system before close the browser. below is the code i only able to come up with.

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script type="text/javascript">

        $(window).bind('beforeunload', function () {
            return 'Please LogOut';
        });

    </script>

I would like to do something like below. Display an confirm box and if the user press OK then make an ajax call.

I tried below code but even the confirm box is not showing up.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script type="text/javascript">

        $(window).bind('beforeunload', function () {
            var x;
            var r = confirm("Please LogOut");
            if (r == true) {
                x = "LogOut";
                var url = "/User/logOut/";
                  $.ajax({
                    url: url,
                    data: { UserID: 1},
                    cache: false,
                    type: "POST",
                    success: function (data) {

                    },
                    error: function (reponse) {
                        alert("error : " + reponse);
                    }
                });
            }
            else {
                x = "Stay On Page!";
            }

        });

    </script>
chamara
  • 12,649
  • 32
  • 134
  • 210
  • check this out http://stackoverflow.com/questions/4146647/destroy-php-session-on-closing – CME64 Jul 01 '13 at 07:25
  • 1
    You may not be able to use the beforeunload event to do this. Each browser behaves differently. Chrome displays your message along with a "Leave this page" and "Stay on Page" options. If the user clicks "Leave this page", the tab/browser closes your code will not get a handle. What you could do is clear all session cookies on close of window – Arun Jul 01 '13 at 07:27
  • Why would you want to force a user to logout? Users don't like being forced you know... Asp.net definitely has a better solution to whatever problem you are facing than relying on users logging out. – nunespascal Jul 01 '13 at 07:39
  • What happens if the browser crashes, or the user ends the process? – Qantas 94 Heavy Jul 01 '13 at 07:40
  • 1
    @Qantas94Heavy actually this is for tracing user activity for an application i have developed. clients wants a report of logged users with their log out time. there can be cases where logout time will not be submitted but i need most of those situations to be handled – chamara Jul 01 '13 at 10:16
  • `onbeforeunload` is called any time the browser tab or window is unloaded. That could because the user closed the tab or window, but it could also be because the user pressed the back button or followed a link on the page. Prompting the user to logout in these latter cases is not only annoying but illogical. – Chris Pratt Jul 01 '13 at 14:53

1 Answers1

1

We tried something similar, but you cannot detect when user closes browser so easily.

Easiest way was to use session as user access. This means that on each call, your client side should send some token and server side should check it. This token is saved on client side within cookie/storage (session storage, but it works only on tab as session) and it will expire as soon as user closes window/tab.

Of course, your server side should support sessions. For tokens sending I suggest using authorization header, since it can be used from most development platforms, if you plan to expand from js to e.g. android platform sometime in the future.

EnterSB
  • 984
  • 2
  • 10
  • 27