Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert when browser window closed accidentally

Tags:

I have a chat application, I want that whenever user accidentally or manually closes the browser ,he should get an alert so that various clean up operations could be carried out. I have more thing i itried to use on before event but I want that to be used for a particular web page as all other web pages are also called on before load. Please help

like image 827
pradeep Avatar asked Aug 07 '09 12:08

pradeep


People also ask

How can I tell if my browser window is closed?

To check if an opened browser window is closed, you can use the closed property in referenced window object in JavaScript. The property returns a boolean true if the window is closed and false if the window is in the opened state.


1 Answers

This is not really a JQuery thing, I use this functions:

function setConfirmUnload(on) {     window.onbeforeunload = (on) ? unloadMessage : null; }  function unloadMessage() {     return "Are you sure you want to leave this page"; } 

Then, at any time you can call

setConfirmUnload(true) to enable the confirmation or false if you want to allow them to close the screen without a warning (if you have a close button for instance).

like image 137
Ariel Popovsky Avatar answered Sep 25 '22 22:09

Ariel Popovsky