2

I'm presently writing JavaScript, on clicking the close button in the window, I should get a confirm box. In the confirm box, I should display some message and there should be cancel and continue buttons.

On clicking cancel, the window should not be closed, but on pressing continue, the window should be redirected to another jsp.

Can someone please help me with this code? I tried using the custom confirm box, but that seems to return only a string and it cannot be used to redirect to a page.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vamsi Varanasi
  • 115
  • 1
  • 1
  • 6

3 Answers3

2

This is impossible. You cannot redirect the user to another page when they close the window. This is for security reasons.

To display a confirm box when closing the window, you can use the onbeforeunload event. This will ask the user if they wish to leave the page or not. The confirm box is rendered by the browser, all you can customize on is the text.

$(window).bind('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

When the user leaves the page, you can use the onunload event, but again, you cannot redirect them (you can make an AJAX call, but you cannot redirect the browser).

$(window).bind('unload', function(){
  console.log('bye'); // Some browsers may block this.
                      // Chrome blocks alerts in this event.
});
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • thanks...I have already tried putting the message , that's not a problem , but i need the continue button in the confirm box to redirect it to another jsp page...is it possible ? if so please tell me how ? – Vamsi Varanasi Feb 23 '12 at 15:27
  • @VamsiVaranasi: No, that's impossible. – gen_Eric Feb 23 '12 at 15:29
  • Can u suggest any other way so that i can get a dialog box when i press the close window button . and that dialog box has two buttons , that i can use to redirect the users on clicking continue to another jsp. – Vamsi Varanasi Feb 23 '12 at 15:34
  • @VamsiVaranasi: No, I can't. It's impossible. – gen_Eric Feb 23 '12 at 15:38
  • @VamsiVaranasi: You're welcome. Unfortunately, you cannot redirect the user when they try to close the page. That's for security, it could be used for malicious purposes. – gen_Eric Feb 23 '12 at 15:41
0

check this example it may help you out

function setConfirmUnload(on){
   window.onbeforeunload = (on) ? unloadMessage : null;
}

function unloadMessage(){
   return "Are you sure you want to leave this page";
}

setConfirmUnload(true) to enable the confirmation or false if you want to allow them to close the screen without a warning (if you have a close button for instance).

You can try also this (link : http://api.jquery.com/unload/)

$(window).unload(function(){
   alert("Bye now!");
}); 
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Sanjay Goswami
  • 1,386
  • 6
  • 13
0

You need to bind a handler to the onBEFOREunload event, as none of the functionality you want will be possible with the onunload event (it is too late).

var myFunction = function () {
    return "Are you sure you want to leave the page?";
};

window.onbeforeunload = myFunction;

As far as I know, there is no way to react to the user input as the confirm box is handled by the browser.

jbabey
  • 45,965
  • 12
  • 71
  • 94