javascript - 在用户离开页面之前显示模态表单

标签 javascript jquery jquery-ui

我使用 window.onbeforeunload 在用户试图离开网站时显示自定义消息。

例子:

window.onbeforeunload = function(){
  if(some_condition){
    return "Are you sure you want to navigate away from this page?\nAll unsaved changes will be lost.";
  }
};


+--------------------------------------------------------+
| Are you sure you want to navigate away from this page? |
| All unsaved changes will be lost.                      |
|                                                        |
|          [ Yes ]  [ Cancel ]                           |
+--------------------------------------------------------+

不过,我想稍微加强一下。如果可能的话,我想使用自定义模式表单而不是通用弹出窗口。

有办法吗?

最佳答案

绑定(bind)到 html 对我来说效果很好,而不是卸载。原因在另一个answer中有很好的解释在这里。

$("html").bind("mouseleave", function () {
    $('#emailSignupModal').modal(); \\or any modal
    $("html").unbind("mouseleave");
});

如果您只想在一天中或在任何其他特定条件匹配时显示模式,那么您可以使用 cookies .

关于javascript - 在用户离开页面之前显示模态表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3927904/

相关文章:

jquery 接下来与 ol

javascript - wkhtmltopdf 和 jQuery.ready() 函数

javascript - 如何在保持相对于另一个 Div 的同时将 HTML 附加到正文

javascript - 响应列和可扩展的 div

javascript - AngularJS ui-grid editableCellTemplate : 'ui-grid/dropdownEditor'

javascript - UI Slider 值显示 "Or more"和 "Or less"

javascript - onbeforeunload jquery 和 css 样式处理程序?

javascript - 谷歌样本上的基本 Action ?

javascript - 带 Electron 的 Angular2 CLI

javascript - 项目符号和编号图标不会出现在 Tinymce 编辑器中