使用javascript自定义beforeunload提示 [英] Custom beforeunload prompt with javascript

查看:380
本文介绍了使用javascript自定义beforeunload提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置退出调查,以便当用户离开我们的结帐页面时,系统会提示他们询问他们为什么要离开.

I'm trying to setup an exit survey so that when a user leaves our checkout page they are prompted asking them why they're leaving.

这可能吗?

我花了一些时间在Google上,但似乎唯一的解决方案是简单的由浏览器控制的类似于确认的提示.这是真的吗?

I've spent some time on Google but it appears as though the only solution is a simple browser-controlled confirm-like prompt. Is this true?

更新

以下确认对话框不会出现,页面只是更改或退出.在等待用户响应之前,如何防止页面更改/退出.我想我应该问,使用e.preventDefault();后如何恢复用户的退出/页面更改操作?

The following confirm dialog never appears, the page just changes or exits. How can I prevent the page from changing/exiting until I wait for a user's response. I guess I should ask, how can I resume a user's exit/page change action after using e.preventDefault(); ?

jQuery(window).bind('beforeunload', function() {
    return function () {
        alert('x');

        setTimeout(50000, function () {
            confirm('you sure?');
        });
    }();
});

推荐答案

如果您使用的是jQuery,则可以这样做

If you are using jQuery you can just do

$(window).unload( function() {
  //statements
});

这篇关于使用javascript自定义beforeunload提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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