我使用 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/