/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Need help cloning document.cookie functionality

Hey guys,

Presently my script stores data in cookies using the standard procedures to set, get and delete cookies, like so:

[CODE]function setCookie(name, value, expires, path, domain, secure)
{
document.cookie= name + “=” + escape(value) +
((expires) ? “; expires=” + expires.toGMTString() : “”) +
((path) ? “; path=” + path : “”) +
((domain) ? “; domain=” + domain : “”) +
((secure) ? “; secure” : “”);
}

function getCookie(name)
{
var dc = document.cookie;
var prefix = name + “=”;
var begin = dc.indexOf(“; ” + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
}
var end = document.cookie.indexOf(“;”, begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain)
{
if (getCookie(name))
{
document.cookie = name + “=” +
((path) ? “; path=” + path : “”) +
((domain) ? “; domain=” + domain : “”) +
“; expires=Thu, 01-Jan-70 00:00:01 GMT”;
}
}[/CODE]

After studying cookies, I see that they have a unique functionality, in that setting the “document.cookie” property will not wipe its current value clean, but instead append or modify the necessary cookie values.

Now, what I’m looking to do is be able to have a variable in my document that will act exactly as “document.cookie” does when setting its value. Can anyone point me in the right direction of establishing this? I will be using ajax to save and recall userdata to a MySQL database. I am trying to clone document.cookie’s behavior as much as possible to make this transition away from cookies easier.

If you need any clarification, let me know. ?

Thanks,

David

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meMay 21.2009 — why would you need difficult-to-reproduce behavior of document.cookie?

most people use setCookie and getCookie to work with cookies, as your script does.

you can strip the contents of those functions and put whatever you like in instead, preserving the external interface that relies upon calling getCookie
Copy linkTweet thisAlerts:
@davidjnelsauthorMay 21.2009 — Thanks, I got it.
×

Success!

Help @davidjnels 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 6.13,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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