cookies shorter than a session?

2

2obvious

I'm trying to set a cookie to expire 10 seconds after it is created.
However, it seems to last until I close all browser windows.

var objDate = new Date();
objDate.setSeconds( objDate.getSeconds() + 10 );
document.cookie = "hasSeenPopup=TRUE;";
document.cookie += "expires=" + objDate.toGMTString();

I then set the cookie to expire in 1000 seconds, to test that it could
indeed last longer than a session. However, when I close all browser
windows, the cookie is gone by the time I reopen the browser, no matter
how quickly I do this.

I am doing something incorrectly?
 
S

Stephen Chalmers

2obvious said:
I'm trying to set a cookie to expire 10 seconds after it is created.
However, it seems to last until I close all browser windows.

var objDate = new Date();
objDate.setSeconds( objDate.getSeconds() + 10 );
document.cookie = "hasSeenPopup=TRUE;";
document.cookie += "expires=" + objDate.toGMTString();

I then set the cookie to expire in 1000 seconds, to test that it could
indeed last longer than a session. However, when I close all browser
windows, the cookie is gone by the time I reopen the browser, no matter
how quickly I do this.

I am doing something incorrectly?

It seems that a date-specified cookie can expire during a session, in as little as 10 seconds if required.

<SCRIPT type='text/javascript'>

document.write( "COOKIE DETECTED: "+ /cookieLife/.test(document.cookie) + "<BR><BR>" );

var exp=new Date(), lifeSecs=10;

document.write("System time: "+exp+"<BR><BR>");

exp.setSeconds(exp.getSeconds()+lifeSecs);

document.write("Setting expiry to system time advanced "+lifeSecs+" secs: " + exp +
"<BR><BR> Reload page to test duration.");

document.cookie="cookieLife=true;expires="+exp.toGMTString();

</SCRIPT>
 
R

RobG

Stephen said:
It seems that a date-specified cookie can expire during a session, in as little as 10 seconds if required.

<SCRIPT type='text/javascript'>

document.write( "COOKIE DETECTED: "+ /cookieLife/.test(document.cookie) + "<BR><BR>" );

var exp=new Date(), lifeSecs=10;

document.write("System time: "+exp+"<BR><BR>");

exp.setSeconds(exp.getSeconds()+lifeSecs);

document.write("Setting expiry to system time advanced "+lifeSecs+" secs: " + exp +
"<BR><BR> Reload page to test duration.");

document.cookie="cookieLife=true;expires="+exp.toGMTString();

</SCRIPT>

That seems to work pretty well in some browsers, but not in Safari.
Is there an open standard or official specification for working with
cookies? Below is a link to Netscape's 'specification' and a script
that works in Safari (as well as any other browser I tested):

<URL:http://wp.netscape.com/newsref/std/cookie_spec.html>

<URL:http://developer.apple.com/internet/safari/faq.html#anchor6>

The Netscape link is provided in the W3C's pages on cookie handling
mechanisms, so I guess it must be as good as you'll get.

<URL:http://www.w3.org/Library/src/HTCookie.html>
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Fri, 5 Aug 2005 02:43:59, seen in Stephen
Chalmers said:
exp.setSeconds(exp.getSeconds()+lifeSecs);

Using that purely as an illustration, since the time taken to do it once
is negligible :

K = 40000 ; D = new Date()
D1 = new Date()
J = K ; while (J--) D.setSeconds(D.getSeconds()+1)
D2 = new Date()
J = K ; while (J--) D.setUTCSeconds(D.getUTCSeconds()+1)
D3 = new Date()
J = K ; while (J--) D.setTime(D.getTime()+1000)
D4 = new Date()
J = K ; while (J--) {}
D5 = new Date()

x = [D2-D1, D3-D2, D4-D3, D5-D4]

gives 12140,1210,770,110

The non-UTC function (no doubt because it considers local time) is about
ten times slower than the UTC function, which is about 1.7 times slower
than directly changing the value. In my system.

Javascript will look a bit odd if the M.....s manage to abolish UTC; JS
should have used GMT instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top