Jquery – Popup before window is closed


I want to open a custom popup box when browser close button is pressed, I want to ask a question to the users exiting that why are they leaving my site. I have checked quite few posts but the best thing I found is something like this post


But this just opens a browser default confirm box. I want to open a custom box like colorbox. My page is http://cwstudio.in/browserclose/

right now I am using alert function to open the popup but if I donot use the alert, the browser donot waits for my popup and just close it.

Any help is appreciated.


Best Solution

You can use the javascipt window.onunload or jquery unload method:


 window.onunload = function(){
 //Do something


 //Do something

Update: If the user has blocked pop-ups then you can not do anything. However you can use a confirm box instead of showing popup:

var r = confirm("Are you sure you want to leave?");
Related Question