用户离开站点时显示消息

发布于 2024-08-31 12:14:02 字数 706 浏览 7 评论 0原文

我正在寻找一种方法,如果用户在仅查看一个页面后离开我的网站,则向用户显示一条消息。

我发现了这个(http://www.pgrs.net/ 2008/1/30/popup-when-leaving-website)聪明的解决方案,但它有一些缺陷:

staying_in_site = false;

Event.observe(document.body, 'click', function(event) {
  if (Event.element(event).tagName == 'A') {
    staying_in_site = true;
  }
});

window.onunload = popup;

function popup() {
  if(staying_in_site) {
    return;
  }
  alert('I see you are leaving the site');
}

它在刷新页面或使用后退按钮时也会显示消息。

您知道更好的解决方案或如何在上面的代码中修复它吗?我不是 javascript 大师:)

我的目的是仅在非常特定的登陆页面上添加代码,并在人们离开该页面而不下载我的试用软件或阅读我网站上的其他页面时显示消息。

I'm looking for a way to display a message to the user if he leaves my site after only viewing one page.

I found this (http://www.pgrs.net/2008/1/30/popup-when-leaving-website) clever solution, but it has a few flaws:

staying_in_site = false;

Event.observe(document.body, 'click', function(event) {
  if (Event.element(event).tagName == 'A') {
    staying_in_site = true;
  }
});

window.onunload = popup;

function popup() {
  if(staying_in_site) {
    return;
  }
  alert('I see you are leaving the site');
}

It displays the message also when refreshing the page or using the back button.

Do you know a better solution or how to fix it in the above code? I'm no javascript master :)

My intention is to add the code on very specific landing pages only, and display the message when people leave the page without downloading my trial software or reading other pages on my site.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦屿孤独相伴 2024-09-07 12:14:02

首先我要说的是,我不会以任何方式建议您这样做。这是一种不好的做法,它只会惹恼用户并且让你看起来非常绝望。我再说一遍,不要这样做,这不是一个好主意,它很愚蠢,也不是一个好主意。

但您可以做到的方法是仅在用户单击离开网站的链接时显示消息。
您可以通过查看链接的 href 属性并检查它是否是外部站点来完成此操作。如果是,则显示消息

但我再说一遍,不要这样做!

I will start by saying that I will not, in any way, recommend that you do this. It's a bad practice, it only annoys users and it makes you look extremely desperate. I repeat, don't do this, it's not a good idea, it's silly and it's not a good idea.

But the way you can do it is to only display the message when users are clicking on links that lead away from the site.
You can do this by looking at the href attribute of the link and check if it's an external site. If it is, then display the message

But I repeat, don't do this!

著墨染雨君画夕 2024-09-07 12:14:02

我们或许可以原谅您的 window.onbeforeunload。但就是这样!

We might be able to forgive you for window.onbeforeunload. But that's it!

焚却相思 2024-09-07 12:14:02

在某些情况下,这可能非常有用,但没有此功能会很烦人。我为客户开发的应用程序之一要求用户在页面上输入大量数据(接近 25 个字段)。我知道如果将表单分成更多页面,效果会更好、更有用,但这不是重点。在某些情况下,员工(大多数不是很懂行的计算机用户)输入了数据,但出于某种原因离开了页面而没有提交(我确信是意外的),导致了挫败感。在这种情况下,我很高兴执行 window.onbeforeunload

There are some cases where this can be very useful and not having this feature will be annoying. One of the applications I developed for a client required users to input a lot of data on a page (close to 25 fields). I know it would have been better and more usable if the form was broken into more pages, but that's not the point here. There were instances where employees (mostly not very savvy computer users) entered data but for whatever reason left the page without submitting (accidentally, I am sure), resulting in frustration. I was happy to do window.onbeforeunload in that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文