0

I know this must be done with javascript. But I am not sure how to do it.

Scenario:

When user close the browser, I will prompt a message whether he/she want to leave comment/feedback or not. If he/she clicks yes, then display(or maybe window.opn/popup), else just close the browser normally.

I tried using onload function, but it does not work in firefox

JS

<script type="text/javascript">

function unloadPage()
{
 alert("unload event detected!");
}
</script>

HTML

<body onunload="unload();">

I also tried the onbeforeunload

window.onbeforeunload = test;
function test() {
return "Submit your feedback to us :)";
}

But it appears that, it just prompt like a normal window.alert. It has "Ok" and "Cancel" button. I cant do much from there.

Can we do a check, if the user clicks "OK", we perform/redirect/popup new window, and if user clicks Cancel, we just close the browser?

I always have the compatibility problems, sometimes works in IE but not in FF

  • 1
    I feel like I don't want to answer this, because I would never want to see that solution put to life anywhere. It would probably piss me off. You could look at having a feedback-link on your page instead. Have a look at www.uservoice.com – peirix Sep 09 '09 at 09:13
  • 1
    Ugh, not another webpage that tries to stop me from closing my browser... – Franci Penov Sep 09 '09 at 09:18
  • this question is answered [here](http://stackoverflow.com/questions/1299662/prompt-user-for-unsaved-changes-when-leaving-webpage/1299671#1299671). – KB22 Sep 09 '09 at 09:12

1 Answers1

1

Can we do a check, if the user clicks "OK", we perform/redirect/popup new window

Use a onbeforeunload and set a timeout just before returning the 'Do you want to blah blah' string. If the timeout fires, the user clicked 'Cancel' to stay on the page and you can take a further action such as adding a form or doing a redirect. Pop-ups will usually be blocked as normal.

This is highly unpleasant behaviour. If you do it, you risk prosecution under the Serious Web Crimes Act and are liable for punishment up to and including death or three months' imprisonment with only Netscape 4 as a browser. (I'd take the death.)

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Are you serious? Add a function in when closing a browser will end up jail or death? So serious??? –  Sep 10 '09 at 03:55
  • No, obviously. But if it stops you making an onunload-moaner then yes. :-) – bobince Sep 10 '09 at 07:38
  • My boss insists want to do. I have no choice.. grrr... I dont know why he cant leave a feedbank link/button, why must perform after closing browser? damn irritating!! –  Sep 10 '09 at 17:01
  • Ah, the Nürnberg defence ;-)... yeah, bosses can demand some pretty dumb things, huh? – bobince Sep 10 '09 at 17:08
  • tried using setTimeout, it does not work. Can you write the sample code? –  Sep 10 '09 at 17:48