Reputation: 794
I want to catch the close event of my xulrunner app when user clicks the close button of the main window and ask them for confirmation.If they dont confirm ,i want to cancel this close event and continue the application.
I tried OnClose Event ,but i was not able to cancel it.
How do i implement this?
Upvotes: 1
Views: 1314
Reputation: 1076
for a programmatic, and DOM level 2-compliant, answer to Nickolay's remark (1):
window.addEventListener(
'close', function (event) {
// do stuff...
event.preventDefault();
},
true);
Upvotes: 0
Reputation: 11
For anyone else looking, you can also do this
put this in the main window
onclose="if(CheckForClose() == false)return false"
create this javascript function somewhere
function CheckForClose()
{
var result = confirm('Do you want to close?');
return result;
}
Upvotes: 1
Reputation: 32073
There are two different things you may want:
1) prompting when a window is closed. You can use the "close" event, e.g.:
<!-- Creates an unclosable window -->
<window onclose="return false" ...>
I just checked it and it does work.
2) prompting when the application is about to quit. You can do this by observing the quit-application-requested notification via the nsIObserverService (example).
(1) is closer to what you asked, since when the user presses the [X] button on the window, the sequence of events is this:
On the other hand, if you put your check solely in the onclose handler of the window, other ways of quitting application (File-Exit on Win/-Quit on Mac) may trigger it too late (e.g. after closing other windows). Also, if your app can have more than one "main" window (with onclose logic), the user will have to deal with multiple confirmations, which may or may not be what you want.
Here are some references to the Firefox code (if I'm not missing something, it should also be packaged with your application), you can research and reuse the approach taken:
Upvotes: 3