Take the 2-minute tour ×

I have a jquery function, it goes like that:

$(window).unload(function() {

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? I tried a lot of things, nothing worked :\

Thanks A lot, and sorry for my English

share|improve this question
add comment

1 Answer

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:


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).

share|improve this answer
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. –  Ariel Aharonson Apr 10 '13 at 14:11
Why don't you put that logic on the server side? On your php controller? –  ricardohdz Apr 10 '13 at 14:58
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 ? –  Ariel Aharonson Apr 11 '13 at 17:06
add comment

Your Answer


