views:

250

answers:

2

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  A: 

Hi,

this question is answered here.

KB22
I tried the onbeforeunload, but it just like normal window.alert. I can add innerHTML, window.location, it doesnot solve my problem :(
I mean "...I cant..."
kay, my question is a bit different from them. I am not returning a string, I want to return some kind of "form"
+1  A: 

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
Are you serious? Add a function in when closing a browser will end up jail or death? So serious???
No, obviously. But if it stops you making an onunload-moaner then yes. :-)
bobince
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!!
Ah, the Nürnberg defence ;-)... yeah, bosses can demand some pretty dumb things, huh?
bobince
tried using setTimeout, it does not work. Can you write the sample code?