HTMHell
HTMHell

Reputation: 6006

jQuery unload() only in a certain case

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?

Upvotes: 0

Views: 169

Answers (1)

ricardohdz
ricardohdz

Reputation: 579

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

Upvotes: 1

Related Questions