views:

1156

answers:

5

Can I tell, using javascript, whether a user has clicked on the "X" icon on a browser dialog, or the "OK"/"Cancel" buttons? I have code I need to run when the window closes, but it will only run when OK or Cancel are clicked.

I currently capture the onunload event of the window. How can i accomplish this?

window.onunload = function() { alert("unloading"); }
+6  A: 

Why do you want to do this? We can probably help you come up with a different design that doesn't require this if you tell us what you're trying to do.

However, to answer your question: it's not possible to catch that event in all cases. You cannot prevent the user from closing the browser or guarantee that your code will execute when they do. You can make it slightly annoying for them, but they can disable javascript, or kill the process, or reboot the computer. Using the unload function is the closest you can come to having some code that runs when the window closes (it will run in cases of normal shutdown or when the user navigates away).

rmeador
@Pwninstein: Just as rmeador says, but in other words: even if you can figure out how, you should not try. If what you want to do has any legitimacy, we will gladly help you. There are guidelines and conventions for how the web works; if you are trying to "mess" with the user, just don't.
Rob Williams
This was for a normal ASP.NET window I was working on earlier. I'm not trying to "mess" with anyone. I just wanted to direct the flow of the dialog (i.e. force them to use the buttons that I provided). I found a way around doing such a thing though. Thanks for the input!
Pwninstein
+3  A: 

What about if he does ALT + F4?

SQLMenace
and what about kill -kill?
bandi
Or if the user pulls the plug on their machine. You can't depend on the client to notify you for this event.
Outlaw Programmer
Could you edit your answer to include bandi and Outlaw's comments? They'll help show that it's not really possible.
Rob Hruska
A: 

It is impossible to catch the closing of the browser and handle every other event that also causes a post back. You can try and many people have before you and failed.

Your best bet is to use onbeforeunload and learn how to deal with session timeouts on your serverside to clean up data.

epascarello
+3  A: 

If I understood correctly, your question is about a browser dialog, not the main browser window.

To answer your question, you probably cannot distinguish between the Cancel button and the X button of a browser dialog. They'll both end up just returning a false. If you need this level of control, you should consider writing your own simulated dialog (lightbox) instead of a real JavaScript dialog. Or perhaps look at existing frameworks/plugins with modal dialogs that give you the amount of control you need.

Ates Goral
+1 - I like this answer. You've bypassed the confusion that the wording of the question gives and caught the key word - 'dialog'...
Steve Perks
+2  A: 

To the best of my knowledge, you can't detect whether the user closed the dialog by clicking the Cancel button or the [x] button, since neither are exposed to you beyond returning the result of the action (e.g., confirm() as true/false).

You can hook into the document.onbeforeunload event to perform whatever cleanup action you require; I've done so myself by sending an asynchronous XMLHTTP request to the server to make sure the user's session gets cleaned up properly.

Christian Nunciato