用户离开网站时显示消息 [英] Display message when user leaves site

查看:80
本文介绍了用户离开网站时显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,如果用户仅查看一页后离开我的网站,就会向其显示消息.

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

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

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.

您知道更好的解决方案,还是如何在上面的代码中对其进行修复?我不是javascript高手:)

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.

推荐答案

我首先要说的是,我绝不会建议您这样做.这是一个不好的做法,它只会使用户烦恼,并且使您看起来极度绝望.我再说一遍,不要这样做,这不是一个好主意,这很愚蠢,也不是一个好主意.

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.

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

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

这篇关于用户离开网站时显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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