[thelist] JS: Cookie Madness

liorean liorean at f2o.org
Tue Feb 3 15:23:27 CST 2004

Tom Dell'Aringa wrote:
> --- Hassan Schroeder <hassan at webtuitive.com> wrote:
>>Uh, `expireDate = new Date()` is *right now* and by the time you
>>set the cookie it's already in the past, eh?
>>Presumably you want it to expire at (now + some_interval)  :-)
> Ah this is true, but not the problem. Revised version below. The
> problem is JS sees the ";" as the end of the STATEMENT and stops
> evaluating. I've done some research via google on this, lots of
> questions but no answers...
> function SetCookie (cookieName, cookieValue, expireDate) 
> {
>    var today = new Date();
>    var expireDate = new Date(today.getTime() + 28 * 24 * 60 * 60 *
> 1000); // plus 28 days
>    document.cookie = cookieName + "=" + escape(cookieValue) + ";
> expires=" + expireDate.toGMTString() + "; path=/";
>    alert(document.cookie)
> } 
> Run this and you will get the name and value but NO expiry date or
> path.

Human logic errors, bah! The name/value pairs is all that is presented 
to you through JavaScript. The expiry time is set, however. You can test 
this by using for example the Mozilla cookies manager, or "View Stored 
Cookies" in Menu:Tools:Options...:Privacy:Cookies if you use Firebird. 
There are equivalents in op7 and iew as well, I think.

You could of course have used an already existing cookies library and 
not at all had to create your own: 

liorean <mailto:liorean at user.bip.net>

ViewStyles, ViewScripts, ToggleStyles and GraphicsInfo bookmarklets and 
Theme Switcher, Cookies Handler scripts:

More information about the thelist mailing list