如何检测回访者并重定向到特定URL? [英] How to detect a returning visitor, and redirect to a specific URL?

查看:80
本文介绍了如何检测回访者并重定向到特定URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个新网站,该网站需要能够检测用户以前是否访问过该网站,然后将其定向到特定的URL(例如example.com/welcomeback.html)。

I am developing a new website that needs the ability to detect if the user has visited the website previously, then direct them to a specific URL (i.e. example.com/welcomeback.html) if they have.

我认为这需要使用cookie和javascript来完成,但是我在任何地方都找不到关于此的任何教程。

I assume that this needs to be done with the usage of cookies and javascript, but I can't find any tutorials on this anywhere.

我们非常感谢您的帮助。谢谢。

The help is much appreciated. Thank you.

推荐答案

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

获取cookie

function getCookie(c_name)
{
   var i,x,y,ARRcookies=document.cookie.split(";");
   for (i=0;i<ARRcookies.length;i++)
    {
       x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
          y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
          x=x.replace(/^\s+|\s+$/g,"");
          if (x==c_name)
          {
              return unescape(y);
          }
    }
 }

if(getCookie('visited'))
{
 location.href="redirecturl";
}else
{
setCookie('visited',1,365);
}

Cookie教程

这篇关于如何检测回访者并重定向到特定URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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