Reputation:
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
Upvotes: 0
Views: 2374
Reputation: 536339
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.)
Upvotes: 1