0

I am developing as MVC 5 application. I want store the users logout time in database. In my web page i have different Links (@Html.ActionLink). In my controller i have written code to store logout time. The same is working. However, the problem is Action method is called even when Link is clicked. So I added flag to check whether link is clicked or not ... No luck ... now logout method is not at all call.

Below is my Java script

$(document).ready(function () {
            var isCliked;
$(".link").click(function () {

            isCliked = "Y";
        });
$(window).bind("beforeunload", function () {

            if (!isCliked == "Y") {
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: '@Url.Action("ClearSession", "Login")',

                    data: {},
                    success: function (d) {
                        alert("About to Close Session");
                        window.location.reload();


                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {

                    }

                })
            };

        });
tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

1

why don't you call the code directly

$(document).ready(function () {

    $(".link").click(function () {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '@Url.Action("ClearSession", "Login")',

            data: {},
            success: function(d) {
                alert("About to Close Session");
                window.location.reload();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {

            }

        });
    });

});
  • Hello Mahamed ... if I call action method directly as suggested by you ,, Method will be called when any link on the page is clicked instead of Browser close event.. As per my understanding beforeunload will be called just before closing the browser. However when the new page is requested through any link inside page, previous page will be closed first. Hence i have to check whether actual browser close is occurred or result of page navigation – Siddhesh Adarkar Mar 13 '16 at 07:38
  • good , i recommend you to check this question [jquery beforeunload](http://stackoverflow.com/questions/18783535/jquery-beforeunload-when-closing-not-leaving-the-page) – Mohamed Allam Mar 13 '16 at 08:11
  • Thanks Friend... It really works.. Just have to turn off & turn on before beforeunload event on mousedown & mouseleave event respectivly – Siddhesh Adarkar Mar 13 '16 at 11:51
  • $(".link").on('mousedown', stopNavigate); $('.link').on('mouseleave',addClose) ; function stopNavigate() { $(window).off('beforeunload'); } function addClose() { $(window).on('beforeunload'); } – Siddhesh Adarkar Mar 13 '16 at 11:53
0

use html

<a href="#" onclick="call function to save logout time"> LogOut</a>
rashfmnb
  • 9,959
  • 4
  • 33
  • 44
  • Hello.. I want to auto logout when browser close event is fired.. No Log out button on the form – Siddhesh Adarkar Mar 13 '16 at 07:57
  • watch this link http://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event and this one https://api.jquery.com/unload/ – rashfmnb Mar 13 '16 at 10:19