Hello,
I have a gallery and I am trying to remember the page in the pagination as well as the image the user is on, if they leave the page so they can come back and pick up where they left off.
I am trying to set and retrieve cookies using JS for the first time here.
I found an example that I am using. Below are the functions I am including in my page in a .js file
[CODE]
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
var temp=””;
function Get_Cookie(name)
{
alert(‘in there’);
var start = document.cookie.indexOf(name+”=”);
var len = start+name.length+1;
if ((!start) && (name != document.cookie.substring(0,name.length)))
return null;
if (start == -1)
return null;
var end = document.cookie.indexOf(“;”,len);
if (end == -1)
end = document.cookie.length;
if((document.cookie.substring(len,end)==””) || (document.cookie.substring(len,end)==”name”)||!document.cookie.substring(len,end))
{
alert(‘please register’);
}
if(name==”imacookie”)
{
temp=document.cookie.substring(len,end);
}
else
{
var temp1= document.cookie.substring(len,end);
document.write(temp1 +” “+temp);
}
return unescape(document.cookie.substring(len,end));
alert(temp);
alert(temp1);
}
function Set_Cookie(name,value,expires,path,domain,secure)
{
document.cookie = name + “=” +escape(value) +
( (expires) ? “;expires=” + expires.toGMTString() : “”) +
( (path) ? “;path=” + path : “”) +
( (domain) ? “;domain=” + domain : “”) +
( (secure) ? “;secure” : “”);
}
Right now, for testing purposes I am calling both the set and get function on the onclick of a thumbnail in my gallery. I can alert that the cookie is being set but the Get_Cookie() is not alerting the temp or temp1 vars which should hold the imageid. It alerts the ‘in there’ alert that I call just inside the function.
I am not sure how this works in JS. I even tried leaving the page and coming back and calling my Get_Cookie() onload because I think I read somewhere that you had to leave the page you set the cookie on…could be wrong on this but I tried it none the less.
Can anyone guide me through this with what I have?
I have a function that I call when you click a thumbnail that does various things on my page but here is how the functions look at the bottom of my function.
[CODE]
Set_Cookie(‘imacookie’,imageid,exp); //imagid is set above in my gallery function
Get_Cookie(‘imacookie’);
Thank you for any help with this.