0

I am using this code in my js file for redirect

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

Problem is my js file execute many time but i want to add some cookie code in js file so that my visitor dont redirect again and again. so what will be cookie code which execute this js file only once unless i change cookie manually

2 Answers2

0

Create your code with this function in mind, using these steps in order. You can check your js reference for how to do each of these steps. I hope this points you in the right direction.

  1. Check for an existing cookie.
  2. If you find the cookie, do not redirect, otherwise, redirect. (This is where your existing line would come in.)
  3. Set a cookie with a timeout value based on how often you want them to redirect.
colonelclick
  • 2,165
  • 2
  • 24
  • 33
0

Not tested

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);
}

Read here.

The Alpha
  • 143,660
  • 29
  • 287
  • 307