2

I want to ask the user why he is leaving my page before he leaves. I tried:

window.onbeforeunload = function() {
   var answer = prompt("Why do you leave?", "");
   if (answer != null) {
       send(answer);
   }
}

But it doesn't work. There are two problems: I should figure out how to display the prompt box to the user, and also how can I send his answer to the server. Do you have any ideas please?

Arnaud
  • 4,884
  • 17
  • 54
  • 85

1 Answers1

3

This is not possible.

From the Mozilla documentation:

Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. See the HTML5 specification for more details.

The best you can do is ask the user for confirmation. This can be achieved by returning a string in the onbeforeunload method:

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};
Kris
  • 2,108
  • 18
  • 19