...

View Full Version : Update a cookie value without changing its expire date?



WA
08-30-2005, 07:59 AM
Hi:
Just wondering, lets say I set a cookie with an expiration date, such as:


document.cookie = "mycookie="+cookievalues+";path=/;expires=" + expireDate.toGMTString()
Now, if i wanted to update the above cookie's value without changing its expire date, am I correct that I should simply set the cookie again with the new value, but without the "expires" part at the end? For example:


document.cookie = "mycookie="+newcookievalues+";path=/"
Will this work in updating the cookie but still have it expire on the original date?

Thanks,

Mr J
08-30-2005, 06:02 PM
You still need the expires part otherwise your cookie will be deleted.

Try this example


<script type="text/javascript">
<!--
expireDate=new Date("dec 25,2005 00:00:00");

function test1(){
cookievalues="Hello World"
document.cookie = "mycookie="+escape(cookievalues)+";path=/;expires=" + expireDate
}

function test2(){
cookievalues="New Cookie Value"
document.cookie = "mycookie="+escape(cookievalues)+";path=/;expires=" + expireDate
}

function test3(){
cookievalues="Help me, I'm melting"
document.cookie = "mycookie="+escape(cookievalues)+";path=/"
}


// -->
</script>

<a href="#null" onclick="test1()">Update Cookie 1</a>
<a href="#null" onclick="test2()">Update Cookie 2</a>
<a href="#null" onclick="test3()">Update Cookie 3</a>When you click Update Cookie 3 the persistant cookie is deleted

WA
08-30-2005, 08:32 PM
Hi MJ:
Thanks for the reply. So there's no easy way to update the value of a permanent cookie without knowing the original expiration date of the cookie? In your example below, var expireDate contains the expire original date. What if I don't know the expiration date, or my script is set up so this date isn't stored in a variable? I was hoping there's just a way to update the cookie value, and leave the expiration date of the cookie in question "as is."

Mr J
08-30-2005, 09:09 PM
Hi George.

So the senario is, if the expiry date was dynamically created using the current date then you would not know the expiry date, hmm.

To my knowledge, which I always class as limited, you cannot access the expiry date in the manner that you want but,

what if you had the expiry date saved to the cookie then when the cookie has been updated have the cookie resaved with that date again?

WA
08-30-2005, 09:16 PM
Hi George.
So the senario is, if the expiry date was dynamically created using the current date then you would not know the expiry date, hmm.


Yep, that's the problem here. :)


what if you had the expiry date saved to the cookie then when the cookie has been updated have the cookie resaved with that date again?

That would be my last resort, but I figured there must be an easier, more elegant way. Originally I thought just by setting a cookie with the same name as an existing one, it overwrites the later with the new value, keeping intact what's not changed, in this case, the expiration date.

Mr J
08-30-2005, 09:28 PM
George.

I'll keep my eyes open for a more elegant way, if you do come across a solution to reference the expiry date please post it.

Thanks

Jeff

Willy Duitt
08-30-2005, 11:44 PM
The expires property is write only...
What you can do is send the expires date as a key/value pair when you set the cookie and then check for the cookie name and split out that key/value pair to use as the new(old) expires date...

Here is a quick example...



<script type="text/javascript">
function setCookie(cookieValue){
var theDate = new Date(new Date().setTime(new Date().getTime()+30*24*60*60*1000)); // 30 days
if(/myCookie/.test(document.cookie)){
theDate = unescape(document.cookie).split('expireDate=')[1]; // split out date to reuse
} document.cookie = 'myCookie='+cookieValue+',expireDate='+theDate+';expires='+theDate;
}
</script>
</HEAD>

<BODY>
<a href="#null" onclick="setCookie('Set Cookie');return false">Set Cookie</a>
<a href="#null" onclick="setCookie('Update Cookie');return false">Update Cookie</a>
<a href="#null" onclick="alert(document.cookie);return false">Alert Cookie</a>


Elsewise, you can try using the date as the cookie name (08/30/2005) and use a regular expression to check for the cookie name and pass that as the expires date when you reset the cookie...

.....Willy

WA
08-31-2005, 08:51 AM
Thanks for the code Willy. I see you're storing the original expiration date explicitly via "expireDate" to access it. I guess there really is no shortcut way to update a cookie value without having to set the expiration date again (using the original date somehow) hehe.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum