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"
<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'>
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')
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.
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.
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.