Welcome to WebmasterWorld Guest from 54.166.117.130

Forum Moderators: open

Javascript Cookie Help

   
7:59 am on May 20, 2007 (gmt 0)

10+ Year Member



I want the script to do the following

1) Get postid through voting function
2) Check to see if the cookie voteids exists
3) If not create cookie voteids with a value of postid with a underscore char
4) if cookie does exit add postid with underscore after the current cookie value

function voting(postid)
{

if (document.cookie.length > 0)
{
var begin = document.cookie.indexOf(voteids+"=");
if ( begin!= -1)
{
begin += voteids.length+1;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = document.cookie.length;
}
var ids=document.cookie.substring(begin, end);
ids +=postid+"_";
document.cookie = "voteids="+ids+"_";
}
else
{
document.cookie = "voteids="+ids+"_";
}

}
else
{
document.cookie = "voteids="+ids+"_";
}

}

5:49 pm on Jun 2, 2007 (gmt 0)

5+ Year Member



This should do the job, and ignore duplicates:

function voting(postid)
{
 var cv, dup=false;

 if(document.cookie && (cv=document.cookie.match(/voteids=([^;]+);*/i)) )
  if(cv[1].match(new RegExp('_?'+postid+'([_;]¦$)')))
   dup=true;
 if(!dup)
  document.cookie='voteids='+(cv?cv[1]+'_'+postid:postid);
}
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month