Detecting when user wants to close the window
Added by bo bosh about 12 years ago
Hi all,
I want to display a confirmation message box saying that there are unsaved data, before he closes the window.
Is there a way to detect when the user clicks on `close` in the browser window?
I have tried adding doJavaScript("alert('message');"); to finalize(), but it hasn't worked.
Thanks.
Replies (5)
RE: Detecting when user wants to close the window - Added by Koen Deforche about 12 years ago
Hey,
You can do this, but the API is a bit different: you need to prepare a message in advance with WApplication::setConfirmCloseMessage()
Regards,
koen
RE: Detecting when user wants to close the window - Added by bo bosh about 12 years ago
Hi Koen,
Once again thanks for the support. However, I am not sure if I have done it right,
I have called setConfirmCloseMessage() from unload(), but it doesn't work.
I looks like the unload function isn't called when user is closing the window.
What am I doing wrong?
Thanks in advance. =]
RE: Detecting when user wants to close the window - Added by Koen Deforche about 12 years ago
Hey,
That's too late indeed.
You need to set the confirm message at the moment that you do not want the user to close the window without a warning (e.g. at the moment that you are showing a form).
Regards,
koen
RE: Detecting when user wants to close the window - Added by bo bosh about 12 years ago
Great, thanks.
Another thing that caught my attention is - the unload function.
I understand that it is invoked when the user navigates away or when he closes the window or tab.
Now I have a static member "std::string last_editor_session",
this member is used to keep on the succession of the user. I mean, in each refresh, I compare the session id in the cookie with
the "last_editor_session" and see if they are equal, if they are, I reset the session id in the cookie and "last_editor_session"
with the new session id using sessionId(), otherwise, setting the login screen.
Now, when the user will quit, I want to set the "last_editor_session" to an empty string, so the next time he will visit the page,
a login screen will be displayed.
I tried to do that using
void myWtApplication::unload() {
last_editor_session = "";
setCookie("session_cookie", "", TIMEOUT_IN_SEC);
}
but it looks like the unload isn't called when the window is closed.
(unload is a virtual function and is at the protected part in myWtApplication class, which inheritors from WApplication class)
Am I doing it wrong?
Thanks again for everything.
RE: Detecting when user wants to close the window - Added by Koen Deforche about 12 years ago
Hey,
As per my previous comment: there is no guarantee that the unload event is called, and there is even less of a guarantee that the cookie that you set in that response is still going to be considered by the browser (since it arrives for a tab/window that has been closed).
I'm not sure what functionality you are trying to achieve, but the combination of static member and cookie looks a bit too complicated to me ?
To persist information for a user across sessions, use a cookie. But I cannot imagine why you would store a session ID, something that is confined by definition to a single session, inside a cookie ?
Regards,
koen