C # MVC Execute Function (LogOut) When Closing the Browser

5

I'm in a service system and I need help to understand how I can call an event (Javascript / jQuery / Not) when a user closes the browser without a "correct" system logout. .

Att.

    
asked by anonymous 15.09.2015 / 20:42

3 answers

4

You can use the jQuery unload () function to capture the moment the < strong> browser is closed, and from that moment you make a call via ajax to the desired Action . Before, you check to see if your browser has been updated or closed for good.

It would look like this:

<script>
            (function ($) {
        var refreshKeyPressed = false;
        var modifierPressed = false;

        var f5key = 116;
        var rkey = 82;
        var modkey = [17, 224, 91, 93];

        //Verifica se submit
        var submitting = false;
        $('form').submit(function (e) {
            submitting = true;
        });

        // verifica as chaves de atualização
        $(document).bind(
            'keydown',
            function (evt) {
                // busca por atualização
                if (evt.which == f5key || window.modifierPressed && evt.which == rkey) {
                    refreshKeyPressed = true;
                }

                // checa se há modificação
                if (modkey.indexOf(evt.which) >= 0) {
                    modifierPressed = true;
                }
            }
        );

        // verifica as chaves de atualização
        $(document).bind(
            'keyup',
            function (evt) {
                // verifica a chave
                if (evt.which == f5key || evt.which == rkey) {
                    refreshKeyPressed = false;
                }

                // checa se há modificação
                if (modkey.indexOf(evt.which) >= 0) {
                    modifierPressed = false;
                }
            }
        );
        $(window).bind('beforeunload', function (event) {
            var message = "not refreshed";

            //submit é falso, realiza ação
            if (!submitting) {
                //Se atualizou entra aqui
                if (refreshKeyPressed) {
                    message = "refreshed";

                    //retorna a mensagem apenas para testes
                    event.returnValue = message;
                    return message;
                }

                //Senão, chama a action
                //$.ajax({
                //    url: BASE_URL + 'Testes/Aqui',
                //    type: 'POST'
                //});

                event.returnValue = message;
                return message;
            }

        });
    }(jQuery));
</script>

The script is self explanatory, but a brief explanation: First it filters the possible update events, such as F5 , etc. After that, check to see if submit of your view . If it is not in any of these clauses, it calls its action via ajax .

Example on JSFiddle .

References:

SOen ,
Soen²

    
15.09.2015 / 21:46
3

Add in your script:

   window.onbeforeunload = function () {
        return "Você realmente deseja fechar?";
    };
    
15.09.2015 / 20:57
1

There is no 100% reliable way to do this as in a desktop system, an alternative in the web environment is to use the unload() event that unhides the page, with some filters getting close to the desired one .

I was having this problem last week I found this code.

Basically what it does is check if the mouse pointer is outside the page area and filters a few keys to not perform the action.

$(window).on('mouseover', (function () {
    window.onbeforeunload = null;
}));
$(window).on('mouseout', (function () {
    window.onbeforeunload = ConfirmLeave;
}));
function ConfirmLeave() {
    return "";
}
var prevKey="";
$(document).keydown(function (e) {            
    if (e.key=="F5") {
        window.onbeforeunload = ConfirmLeave;
    }
    else if (e.key.toUpperCase() == "W" && prevKey == "CONTROL") {                
        window.onbeforeunload = ConfirmLeave;   
    }
    else if (e.key.toUpperCase() == "R" && prevKey == "CONTROL") {
        window.onbeforeunload = ConfirmLeave;
    }
    else if (e.key.toUpperCase() == "F4" && (prevKey == "ALT" || prevKey == "CONTROL")) {
        window.onbeforeunload = ConfirmLeave;
    }
    prevKey = e.key.toUpperCase();
});

The code was taken from: SOen - Trying to detect browser close event

    
16.09.2015 / 22:57