1

What I'm trying to accomplish is the following:

If the user tries to close the window the alert pops up and I want to change the text so a user has the option to leave the page or can choose to stay and redirect to another page.

Probably searching for it in the wrong way but I really can't find anything that works.

What I have so far:

window.onbeforeunload = function() { /*...*/ }

I need to know how to adjust the text of the alert and add a handler on the stay button.

Steeve Pitis
  • 4,283
  • 1
  • 21
  • 24
Graham
  • 1,850
  • 4
  • 21
  • 41

1 Answers1

0

For altering the text I dont know exactly how to help you, but you could use the confirm() function from the window object.

This would look like this:

function myFunction() {
  var answer;
  var result = confirm("Press a button!");
  if (result === true) {
      answer = "You pressed OK!";
  }   else {
      answer = "You pressed Cancel!";
  }
  alert(answer);
}

Refer to this post.

UPDATE: onbeforeunload blocks the dialog. A much simpler solution would be to return something from the event, then you'd get asked in any case:

window.onbeforeunload = function(){
  return "";
};
//Are you sure you want to leave?
bash0ne
  • 394
  • 3
  • 14