Onbeforeunload on Google Core, any hiccups?

Asked

Viewed 302 times

-2

I need to use onbeforeunload for my control of users on my system, IE is working perfectly, but in Chrome it does not work, I need that when closing Chrome it redirects to the page verifies.php that updates the login database to dropped, because if the user closes Chrome without logging in, it hangs login, someone knows how to use it in Chrome currently?

  • 2

    It is not that you "Need to onbeforeunload". What you need is to change the logic to something that does not depend on any action for the login to be released. For example, last access timeout (but there are many other techniques). The "onbeforeunload" can serve as an auxiliary feature to streamline the user process that did not logout, but it will act in a very restricted amount of situations.

  • Leo as @Bacco said, the logic needs to change, my suggestion that I have already mentioned in different responses is the "timer". Mainly beforeunload is not and never has been to detect window closing, but rather it detects page unloading, either by Reload, paging or closing and there is no alternative or specific function to do this.

  • @Then, I will implement the timer, but I don’t think it is a 100% effective alternative, because if the user closes the tab/browser the same will have to wait x m/s to be able to log in again, in case capture the action to close the browser and send the update query to the bank would be perfect for me so if it closes the browser automatically can login again.

  • @Leo is the only effective if compared to the beforeunload, that is to say of all that has this is the most efficient, but nothing will be 100% efficient ... now about waiting minutes to log this is ERROR IN YOUR LOGIC, the idea of the timer is to shift, if it opens before this time it already has to be logged in and should not need to log in again, this is something else and has nothing to do with the dislocation and yes problem in how you defined what is a "user session"... What would happen if the user logged in to 2 pcs"] ...

  • ... By its logic it could not even, what is a problem, imagine q the PC of this defect and did not call more and you did not have the timer, then the person could not even log in the next PC, because by its logic the user is "locked" in the previous session.

  • @Guilhermenascimento ah, truth, I misexpressed my logic, really it can log in if the timer depresses it by inactivity, this solves almost completely my problem, I will implement the timer and take the test if it closes the browser if it stays logged in(which in case should keep), in my system he can not log with the same account anywhere, if given any problem the timer will depress it and he can work elsewhere, thanks for the help, helped me to see the problem from another angle, I was thinking a lot about the way I wanted to implement.

Show 1 more comment

1 answer

0

You will find several responses discouraging this practice, because the user can turn off the computer, lack light, etc. But I use a solution that works well for me, search by Navigator.sendBeacon for more details:

    window.onbeforeunload = function() {
        try {
            var url = 'url.php',
                data = new FormData();

            data.append('funcao', 'executarAlgoNoServidor');
            navigator.sendBeacon(url, data);
        } catch (e) {
            $.ajax({
                url: 'url.php',
                type: 'POST',
                data: { funcao: 'executarAlgoNoServidor' }
            });
        }
    };
  • 2

    In case of power loss/light will not solve this, if switch off directly also not. beforeunload will also not work if the browser causes a crash and close, or if another process kills the browser process.

  • 1

    I do not understand how that could solve the problem of the author of the question. It seems to me that it is just one more way to have the same bug, only with another more advanced technique (in the sense that, any of the things that the author said were problem will cause the same problem in its solution).

  • @Bacco I had a similar problem to the one reported, I have a legacy intranet system a few years ago that uses this feature to modify certain information in the database to release some resource/system. With the Chrome update in December, it stopped working, as it is a controllable intranet system, where all users are accessible next door or a phone call by extension, the above solution was used, by having been quickly implemented and maintained the previous functioning. If it were something more complex, would spend some time researching how to use another option.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.