Detect click on window close 'X' button in Firefox?

Is it possible to detect if a user has clicked the window’s ‘X’ button to close the window in Firefox? Not unloading or anything, just whether or not they clicked that window button?

I was thinking of using an event handler or an event listener but I don’t know exactly what I’d be listening for since I don’t know how or what that button is “reported” as being.

Anybody have any ideas?

  • PBME

Not with javascript.

You would need something running on the local computer to detect that. There is no way to tell from a web page as to what the reason is why a page is being unloaded.


just thinking as I type here…
what about using the x and y loc of the cursor when clicked with a listener? I’m wondering if there are any quirks to reporting cursor location when over the ‘X’ button. In theory, wouldn’t checking against that on the click event work if there were numbers reported that you would not normally find when the cursor is elsewhere? - just a thought.

  • PBME

I’m wondering if there are any quirks to reporting cursor location when over the ‘X’ button.

Try it. Write a small script that displays the cursor position as you move around the window.

What about ALT-F4 which is the keyboard equivalent of clicking on the close button?

You don’t say why you want to do this, but see if this code is of any use.
I haven’t tested it online, but it attempts to ask the user for feedback on window closure, then submits the entered text. You’ll need to configure the form to match whatever feedback form script you have available.
It triggers with I.E. and Mozilla

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<TITLE>Window Close Alert and Feedback</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
Close the window...

<FORM name='feedback' action='myfile.php'>
 <input type='hidden' value='' name='talk'>

<SCRIPT type='text/javascript'>

function exitAlert()
 var quitReason='';

 for(var i=0; i<arguments.length && arguments[i]!=false; i++)

  quitReason=prompt("Please tell us why you are abandoning this page?","It\\'s garbage.");

 if(typeof quitReason != 'string')

 return quitReason;

window.onbeforeunload = function(evt)
 if (typeof evt == 'undefined')
   evt = window.event;

 var response=exitAlert(true,true); /*To activate prompt, all parameters (if any) must evaluate true.*/

   evt.returnValue = response;;


Logic Ali, thanks for that, I’ll try it out. To give some insight as to why I’m trying to do this, it is much as you’ve described, to get feedback from the visitor (I.E. “did you find what you were looking for”, “Is there anything else we can help you with”, etc.).

Thanks again, much appreciated. I’ll let you know how it goes.

  • PBME

There is one problem with the code snippet above that I can see. If the visitor is following links within the page (which in this case are all within our site), the alert still comes up the same as it would if the window is being closed. I’m thinking that I need to add a test to that script for the indexof location to make sure that they are continuing on in the process (remaining at our domain) and not trigger the alert if they are simply going to the next page and staying.

That’s the purpose of the parameter(s) passed to exitAlert(). For the alert to appear, any parameters passed must all evaluate true, therefore they should represent the result of a suitable test. Such a test could be the expiry of a timer, the focusing of a form field or some other action depending upon the purpose of the page.

When i click the window close button, it is working fine. I have submit button in my page. But when i click this button, i’m unable escape from the alert.

Please advice me to solve this issue.

Waiting for your feedback ASAP.

Try this. Not tested, but it may cancel the onunload action.

myForm.onsubmit = function() {
window.onunload = void(null);
document.body.onunload = void(null);

If that doesn’t work, maybe some variation on the same idea will work.

How have you defined your void() function that goes with that code?

I’ve never had to define it. As near as I can tell, it’s an undocumented built-in “do nothing” function. I saw it used in many different scripts back in the day, so I started using it myself.

null by itself does that without needing to use anything that is undocumented and may or may not exist in different browsers.

I’ve never seen it not work in any browser, nor even generate an error message, but you’re right. It might be interesting to test the typeof void() in various browsers to see if it’s really a function. :smiley:

void isn’t undocumented.

All it does is suppress the return value of whatever is inside it and it returns undefined instead.

I find it useful when typing something like this into the address bar:


The reason for that being that I sometimes want to get rid of something quickly and doing it without void() makes the page unload and go to a blank page showing the text “none”. If you force it to return undefined, it doesn’t go anywhere else. It’s not a particularly useful example, but that’s pretty much the only place I’ve ever used it.

Did you mean to say that it is documented? The fact that Mozilla’s JavaScript reference has a page about it sort of suggests that it is, no? :smiley:

“not undocumented” == “documented”, no?

D’oh! :blush: