简体   繁体   English

如何在卸载前向用户显示提示框?

[英]How can I display a prompt box to the user before unloading?

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? 请问您有什么想法吗?

This is not possible. 这是不可能的。

From the Mozilla documentation : Mozilla文档中

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. 从2011年5月25日开始,HTML5规范规定在此事件期间可以忽略对window.alert(),window.confirm()和window.prompt()方法的调用。 See the HTML5 specification for more details. 有关更多详细信息,请参见HTML5规范。

The best you can do is ask the user for confirmation. 您能做的最好的就是请用户确认。 This can be achieved by returning a string in the onbeforeunload method: 这可以通过在onbeforeunload方法中返回一个字符串来实现:

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM