View Full Version : Closing Window

01-17-2005, 04:26 AM
May I know which Javascript function detects when user press the X (close) button? I tried using the onbeforeunload event but seems like this works when user clicks the Back, Forward, Refresh, Close Window or Home button.

I want it to be only for the X (Close Window) button. How can I do so?

01-17-2005, 05:33 AM
There is really no cross-browser solution to detect when the user presses the X button. There is a trick in IE but you can only detect pressing it but you cannot cancel closing the browser like onbeforeunload does. BTW, what's your ultimate reason for detecting the closing of browser? There may be better solution for that.

01-17-2005, 08:11 AM
Well, I am doing a chat program and when user closes the main window by pressing the X button, it will prompt user if he wants to log out or not. That's my main purpose. How can I do this then?

01-17-2005, 08:25 AM
I have seen something do that before however I forget where I've seen it.

01-17-2005, 11:45 AM
function doUnload()
if ((window.event.clientX < 0) && (window.event.clientY < 0))
//open the logout page that auto-closes
window.open("logout.xxx", "", "width=200, height=20");
window.onunload = doUnload;

As I said, this is for IE only and must be used with caution as this can be annoying to some users.