0

I have a jquery function, it goes like that:

$(window).unload(function() {
    chat.logout();
});

I want to active the other function, chat.logout(); only when the user leaves the page, or when the link target (href) is not play.php?mod=.... I don't want that the function will run on page refresh or any other case.

How can I do that?

1 Answer 1

1

For security reasons, there are no ways to determine the target URL the user wants to navigate to. So, you can't apply that logic on the unload handler.

Although, you can get current location with:

document.location.href

I would recommend you to just follow the standards and include a "logout" link, so you can close the chat when the user clicks on it and not only when the user closes the window or navigates to another page (see how Google and Facebook implement their sessions).

Sign up to request clarification or add additional context in comments.

3 Comments

The problem is that in every other GET in the page play.php, the chat is different, it's other room. when the page loads, it will update that you're in room x and not y. so I want to logout the user when he navigate to other page, not play.php, or when he leaves the page. If I use the function unload(), when the user refresh the page, he becomes offline, although I update the db using php every refresh, so I don't want the script will run in a refresh too.
Why don't you put that logic on the server side? On your php controller?
Because I have to tell the server side that the user logged out from the chat, and I have to do it with client-side. I want that the list will be updated on time. If it's not possible, what is the best way to check who's online ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.