基于 Cookie 的重定向功能


Redirect Function Based on Cookie

我在我的js文件中使用此代码进行重定向

setTimeout('top.location=''http://google.com/?123'';', 1000);

问题是我的js文件执行了很多次,但我想在js文件中添加一些cookie代码,这样我的访问者就不会一次又一次地重定向。 那么,除非我手动更改cookie,否则仅执行一次此JS文件的cookie代码是什么

创建代码时考虑此函数,按顺序使用这些步骤。您可以查看您的 js 参考,了解如何执行这些步骤中的每一个。我希望这能为你指明正确的方向。

  1. 检查现有 Cookie。
  2. 如果您找到 cookie,请不要重定向,否则重定向。(这是您现有生产线的用武之地。
  3. 根据您希望它们重定向的频率设置具有超时值的 Cookie。

未测试

function createCookie(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
if(readCookie('redirected')!=true)
{
    createCookie('redirected',true,0); // cookie will be trashed after the browser closed.
    setTimeout('top.location=''http://google.com/?123'';', 1000);
}

在这里阅读。