在用户离开页面之前显示模态表单 [英] Display modal form before user leaves page

查看:63
本文介绍了在用户离开页面之前显示模态表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



<例子:

  window.onbeforeunload = function(){
if(some_condition){
return 你确定要离开这个页面吗?\ n所有未保存的更改都将丢失。;
}
};


+ ------------------------------------- ------------------- +
|您确定要离开此页面吗? |
|所有未保存的更改都将丢失。 |
| |
| [是] [取消] |
+ --------------------------------------------- ----------- +

但是,我想要加强这个一点点。如果可能的话,我想使用自定义模式表单而不是通用弹出窗口。



有没有办法做到这一点?

解决方案

<当用户试图离开时,将激活一个href =http://api.jquery.com/unload/ =noreferrer>卸载事件。但是,如果您使用DIV作为弹出窗口,浏览器将在用户有机会阅读之前离开。



要将它们保留在那里,您必须拥有它使用警报/提示/确认对话框。 (据我所知)


I've used window.onbeforeunload to display a custom message when a user attempts to leave a site.

Example:

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 ]                           |
+--------------------------------------------------------+

However, I'd like to enhance this a bit. If possible, I'd like to use a custom modal form instead of the generic popup.

Is there a way to do this?

解决方案

The unload event will fire when a user tries to navigate away. However, if you use a DIV as a pop-up the browser will navigate away before the user has a chance to read it.

To keep them there you'd have to use a alert/prompt/confirm dialog boxes. (as far as I know)

这篇关于在用户离开页面之前显示模态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆