简体   繁体   English

离开页面前保存用户电子邮件

[英]save user email before leaving page

On the cart page of my website, I have to intercept the user leaving the page and ask them if they want to save their cart via email. 在我网站的购物车页面上,我必须拦截离开页面的用户,并询问他们是否要通过电子邮件保存购物车。

I guess I have to use the event "beforeunload" to intercept the user leaving the page, but then I have two problems: 我想我必须使用事件“ beforeunload”来拦截离开页面的用户,但是然后有两个问题:

  • How to exclude from the "beforeunload" trigger the click on the link to proceed with the payment? 如何从“ beforeunload”触发器中排除点击链接以继续付款?

  • How to prompt a small form where I can ask for his email (to be used somehow later) and then proceed with the unload of the page? 如何在我可以索要他的电子邮件的情况下提示一个小表格(稍后以某种方式使用),然后继续卸载页面?

For excluding on the link to proceed with the payment, you can do this :- 要排除继续付款的链接,您可以执行以下操作:-

window.onbeforeunload = function() {
    return "You're leaving the site.";
};
$(document).ready(function() {
    $('a[rel!=ext]').click(function() { window.onbeforeunload = null; });
    $('form').submit(function() { window.onbeforeunload = null; });
});

The only thing u can do is making a default browser messagebox appears... 您唯一能做的就是显示默认的浏览器消息框...

window.onbeforeunload = foo;

function foo(e) {
        if (!e) e = window.event;
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog

        //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }
}

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

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