且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-23 09:24:03

卸载事件。但是,如果您使用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)