简体   繁体   English

离开网站时弹出

[英]Pop-up when leaving website

I've been asked to have a pop-up when visitors leave the site asking them if they really want to leave. 当访问者离开网站询问他们是否真的要离开时,有人要求我弹出一个窗口。 This pop-up will only show if their shopping cart has items in it. 仅当其购物车中有商品时,此弹出窗口才会显示。

I can easily limit the pop-up to when the cart has items, however the issue I'm having is that even clicking an internal link loads the pop-up - how can I have it so this only comes up when actually leaving the site. 我可以轻松地将弹出窗口限制在购物车中有物品的时候,但是我遇到的问题是,即使单击内部链接也可以加载弹出窗口-我怎么拥有它,因此只有在实际离开网站时才会出现。

<script language="JavaScript">
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "some message about leaving";
  }
</script>

Alright first of all: Don't do this. 好吧,首先:不要这样做。 Please. 请。 It's super-annoying for users. 对用户而言,这是超级烦人的事情。 Just make sure the shopping cart items are stored on the server or in a cookie so users can always go back to the site. 只需确保购物车中的商品存储在服务器上或cookie中即可,这样用户就可以随时返回该站点。

Looking at this related question: How can i get the destination url in javascript onbeforeunload event? 看着这个相关问题: 如何在javascript onbeforeunload事件中获取目标URL? it can't be done easily. 这不容易做到。

Instead of using onbeforeunload , either attach a click handler to external links on your site that shows the popup, or attach a click handler to all links that checks if the link is external or not. 除了使用onbeforeunload ,还可以将点击处理程序附加到显示弹出窗口的网站上的外部链接,或者将点击处理程序附加到检查链接是否为外部的所有链接。

Again, don't do this... 再次,不要这样做...

If a link is clicked, it will tell you in e.target.activeElement . 如果单击链接,它将在e.target.activeElement告诉您。 You can check if it's a link there: 您可以检查是否存在链接:

window.onbeforeunload = confirmExit;
function confirmExit(e)
{
    var $element = $(e.target.activeElement);
    if ($element.prop("tagName") !== "A") {
        return "some message about leaving";
    }
}

Note: You can add additional conditions checking $element.attr("href") to make sure it displays the message for links that aren't your site. 注意:您可以添加其他条件来检查$element.attr("href") ,以确保它显示的消息不是您的站点的链接。

You could get the URL of a clicked link item, and check if it's on the same domain. 您可以获取单击的链接项的URL,并检查它是否在同一域中。 Put this in an if not statement, with the current code inside. 将其放入if语句中,并放入当前代码。

you'll have to control it to enable and disable the behavior, something like this: 您必须控制它以启用和禁用行为,如下所示:

<script>
  var beforeunload = function (event) {
    var message = 'some message about leaving';

    (event || window.event).returnValue = message; // Gecko + IE
    return message;                                // Webkit, Safari, Chrome...
  };

  document.addEventListener('click', function(event) {
    if (event.target.tagName === 'A') {
      window.removeEventListener('beforeunload', beforeunload);
    }
  });

  window.addEventListener('beforeunload', beforeunload);
</script>

this is going to remove the beforeunload event whenever a link is clicked on the page. 每当点击页面上的链接时,这将删除beforeunload事件。

One way, and again, I wouldn't recommend doing this either - the user should be able to leave your site without receiving a warning - but you could unregister the event if a link has been clicked: 一种方式,一种方式,我也不建议您这样做-用户应该可以在不收到警告的情况下离开您的网站-但如果单击了链接,则可以注销该事件:

$('a').click(function() {
    window.onbeforeunload = null;
    return true; // continue
});

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

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