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.
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.
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"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML>
<HEAD>
<TITLE>Window Close Alert and Feedback</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
</HEAD>
<BODY>
Close the window...
<FORM name='feedback' action='myfile.php'>
<input type='hidden' value='' name='talk'>
</FORM>
<SCRIPT type='text/javascript'>
function exitAlert()
{
var quitReason='';
for(var i=0; i<arguments.length && arguments[i]!=false; i++)
;
if(i==arguments.length)
quitReason=prompt("Please tell us why you are abandoning this page?","It\\'s garbage.");
if(typeof quitReason != 'string')
quitReason='';
else
if(quitReason.length>0)
{
document.forms.feedback.talk.value=quitReason.replace(/\\s/g,"_");
document.forms.feedback.submit();
}
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.*/
if(evt)
evt.returnValue = response;
document.forms.feedback.submit();
}
</SCRIPT>
</BODY>
</HTML>
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.
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.
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.
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.
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.