简体   繁体   English

使用javascript自定义beforeunload提示

[英]Custom beforeunload prompt with javascript

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. 我正在尝试设置退出调查,以便当用户离开我们的结帐页面时提示他们询问他们为什么要离开。

Is this possible? 这可能吗?

I've spent some time on Google but it appears as though the only solution is a simple browser-controlled confirm-like prompt. 我花了一些时间在Google上,但似乎唯一的解决方案是简单的由浏览器控制的类似于确认的提示。 Is this true? 这是真的?

update 更新

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(); 我想我应该问,在使用e.preventDefault();之后如何恢复用户的退出/页面更改操作e.preventDefault(); ?

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

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

If you are using jQuery you can just do 如果您使用的是jQuery,则可以

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

You can do anything you want in the onunload event of the browser: 您可以在浏览器的onunload事件中执行任何操作:

body.onunload = function() {
  foo();
  bar();
}

如levu所建议,将onunload事件与自定义模式对话框结合使用,例如thickbox- http://jquery.com/demo/thickbox/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM