3
I need the user to close the browser or the page tab, update the status Logged in that is in the database (to control user access).
For this use the following
Method JavaScript
:
window.onbeforeunload = function (event) {
PageMethods.LogOut();
}
And the next WebMethod
:
[System.Web.Services.WebMethod]
public static void LogOut()
{
//Atualiza status do usuário
}
The problem is that this method is called at all times, when I change the page, when I update the page(F5), how do I fix this?
Perhaps it’s best to switch the event to window.onclose = Function(){}, more context in this link
– h3nr1ke
I tried with onclose, but it doesn’t work.
– Gabriel Santos Reis
@Gabrielsantosreis, what you can do is add a Library to the
a
and ofinput:submit
, as well as in the keypress to monitorF5 (keycode: 116)
, if something like this occurs you set a flag stating thatLogOut
should not be called... Unfortunately this approach will only minimize your problem as a manual refresh (browser button) will trigger the Logout.– Tobias Mesquita
@Tobymosque, really the F5 is the least of my problems, it helps me very little.
– Gabriel Santos Reis
I saw this example, tested in jsfiddle and apparently works, except when you close the whole browser....
– h3nr1ke
It is not possible to detect the closing of windows with javascript, this event detects downloading that is different from closing, that is every closure makes the unonload, but not every unonload comes from closures, here I explained well how this occurs and when we can use it http://answall.com/a/113155/3635 --- I also mentioned about the use of
window.popstate
, using it you could be able to differentiate pagination from some closures– Guilherme Nascimento