How to determine if browser is closed using jQuery

I need to determine if the browser is closing using jQuery, and do an event if this is true. I have a chat function on my website (just like on Facebook) that forces the user to log in when they move to other pages in the website structure, but they need to withdraw them from the chat (so that they don’t displayed on the Internet to others) if they click the close button on browsers. Therefore .unload() will not work because .unload () will apply the exit function to all actions of the window, including switching to another page in the structure of the website. .unload() works to close the browser, but also works to fast-forward and rewind the browser, so I need to find out if the browser is closed and ONLY if the browser is closed.

+4
source share
3 answers

No matter what you use for chat (orbited, websockets?), You should be able to detect the session timeout.

+1
source

The best way to do this is to set up a regular server polling to let him know that the user is still on the page. You can send an AJAX request once a minute to confirm that the user is still there, and log out from the server side if more time has passed since the last notification.

This may not be the most reliable solution, but it is very common and is likely to be effective for what you want to do.

+2
source

It's impossible. onunload / onbeforeunload are the only events the system offers. The reason the page is being unloaded does not appear on the page.

You can often check if a window open for your script exists. These solutions work based on the setInterval() property and the closed window. But I do not think that this is what you are looking for.

+1
source

All Articles