/    Sign up×
Community /Pin to ProfileBookmark

Get_Cookie() problem

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” : “”);

}[/CODE]

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’);[/CODE]

Thank you for any help with this.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@tripwaterauthorJan 26.2007 — Nevermind...

I found a different solution here http://www.elated.com/articles/javascript-and-cookies/

That worked like a charm.
×

Success!

Help @tripwater spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...