PDA

View Full Version : Permanent and Session-Only Cookies



tech_support
07-08-2006, 07:10 AM
Hi,

Can anyone change this script to pernament cookies?



function cookieSave(name, text) {
document.cookie = name + "=" + escape(text);
}

function cookieLoad(name) {
var search = name + "=";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search);
if (offset != -1) {
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) {
end = document.cookie.length;
}
return unescape(document.cookie.substring(offset, end));
}
}
}


Somewhere in my mind is the solution to this problem... but I can't think of it right now :D

Thanks :)

jscheuer1
07-08-2006, 07:45 AM
There are no truly permanent javascript cookies. Cookies can be refused or cleared by the user. The user could use a different browser or computer on subsequent visits. You can set a cookie with a long, long expiration and, as long as it is accepted by the browser and not cleared by the user, it will persist until its expiration for that browser on that computer. If you want something more permanent or user specific, use server side cookies or database with user login. The cookie unit at quirksmode.org is decent and allows for setting optional expiration:

http://www.quirksmode.org/js/cookies.html

The actual code for the cookie unit is about half way down the rather long page at the above link.

tech_support
07-08-2006, 07:57 AM
Thanks :)

And I only want to create a fake "auto-save" in case people press the Refresh button or the Restart button or close their browser or whatever. (OR get a BSoD)