homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Cookie expiry time
greencode




msg:4457929
 7:48 pm on May 25, 2012 (gmt 0)

I'm struggling a bit here. I found this code and I can't figure out how to be in total control of how long the cookie is stored for on the users machine. I've set it up but it seems to expire around 5 years time and I've no real idea why?!

I have this as as the cookie script:


<script>

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.toGMTString())
}

</script>


and then when the user clicks this button it stores the cookie.


<a href="#" onClick="SetCookie('mycookie','mycookie','30')">Allow Cookies</a>


I just need to know how to set the expiry date for 365 days time?

Any ideas, help or advice would be much appreciated.

 

Fotiman




msg:4458794
 11:48 pm on May 28, 2012 (gmt 0)

toGMTString is deprecated and should not be used. Use toUTCString instead.
Instead of setDate, use setTime:

exdate.setTime(exdate.getTime() + (1000 * 60 * 60 * 24 * expiredays));

setTime takes a value in ms, and getTime returns a value in ms. So get the current time in ms, then multiply your expiredays value by 24 to get the number of hours, multiply that by 60 to get the number of minutes, multiply that by 60 to get the number of seconds, and multiply that by 1000 to get the number of ms. Then, as I mentioned, use toUTCString instead of toGMTString.

Hope that helps.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved