|
|
Persistent cookies via Javascript
Hi,
I'm writing an app that requires setting cookies via Javascript.
Problem is that, using Internet Explorer, although I can set and
retrieve them during a browser session, they disappear upon closing
the browser, even when I set an expiry date a significant length of
time into the future (days, years, whatever...).
This doesn't appear to make any difference whether the site is
accessed on localhost or on a remote server.
(Actually, the real problem is that different IE6 windows don't share
cookies- which I need- but this appears to be down to them being
treated as session cookies rather than persistent ones(?))
Here's an example of the code. The setCookie function is a cut-and-
paste example from elsewhere, but it didn't work when I simply set
document.cookie = "whatever=something" either...
<html>
<head>
<script type='text/javascript'>
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
document.write("<br /><br />{" + document.cookie + "}<br />");
setCookie("yellow", "jello", 12);
document.write("<br />{" + document.cookie + "}<br /><br />");
</script>
</head>
</html>
Any help appreciated, thanks.
- MM
|
|
0
|
|
|
|
Reply
|
Metre
|
9/6/2010 11:43:00 PM |
|
Op 07-09-10 01:43, Metre Meter schreef:
> Hi,
>
> I'm writing an app that requires setting cookies via Javascript.
> Problem is that, using Internet Explorer, although I can set and
> retrieve them during a browser session, they disappear upon closing
> the browser, even when I set an expiry date a significant length of
> time into the future (days, years, whatever...).
>
> This doesn't appear to make any difference whether the site is
> accessed on localhost or on a remote server.
>
> (Actually, the real problem is that different IE6 windows don't share
> cookies- which I need- but this appears to be down to them being
> treated as session cookies rather than persistent ones(?))
>
IE6 is old, try to install a newer version?
> Here's an example of the code. The setCookie function is a cut-and-
> paste example from elsewhere, but it didn't work when I simply set
> document.cookie = "whatever=something" either...
>
<cut>copy of some code from:
http://www.w3schools.com/JS/js_cookies.asp
</cut>
>
> Any help appreciated, thanks.
>
> - MM
This example works in IE8 (Win7)
--
Luuk
|
|
0
|
|
|
|
Reply
|
Luuk
|
9/7/2010 8:16:53 AM
|
|
On Sep 7, 9:16=A0am, Luuk <L...@invalid.lan> wrote:
> Op 07-09-10 01:43, Metre Meter schreef:
> IE6 is old, try to install a newer version?
That's not a practical solution in this case. Fortunately it wasn't
necessary because...
> This example works in IE8 (Win7)
Yes, both the example listing given on that page (where I got the
setCookie function from originally) works in IE8 *and* IE6. However...
the example I posted above (using w3schools' setCookie) also worked in
IE6 after all. :-O
While it's not clear what the problem was, it's fairly likely that it
was an error on my part (not clearing the cache properly?). Sorry for
wasting your time folks. :-(
- MM
|
|
0
|
|
|
|
Reply
|
Metre
|
9/7/2010 10:09:23 AM
|
|
|
2 Replies
177 Views
(page loaded in 0.065 seconds)
|
|
|
|
|
|
|
|
|