set cookie expire date

Discussion in 'JavaScript' started by driesdk, Aug 18, 2006.

  1. #1
    hey, I have this script which set a cookie, but the expiring of the cookie is session only.
    Can somebody help my modifie this script so the cookie is good for a long time (maybe a few months, or a specific date like 2170 or so?
    this is the code I have
    <script type="text/javascript">
    	function cookieSave(name, text) {
    		document.cookie = name + "=" + escape(text);
    	}
    
    	function cookieLoad(name) {
    		var search = name + "=";
    		if (document.cookie.length > 0) {
    			offset = document.cookie.indexOf(search);
    			if (offset != -1) {
    				offset += search.length;
    				end = document.cookie.indexOf(";", offset);
    				if (end == -1) {
    					end = document.cookie.length;
    				}
    				return unescape(document.cookie.substring(offset, end));
    			}
    		}
    	}
      </script>
    Code (markup):
    Thanks, Dries
     
    driesdk, Aug 18, 2006 IP
  2. Darrin

    Darrin Peon

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try something like:

    var now= new Date()
    var expireDate= new Date()
    expireDate.setTime(now.getTime() + 60*60*24*365*1000) //1000 because time is in milliseconds
    setCookie("http://www.mysite.com", name, expireDate)

    -Darrin
     
    Darrin, Aug 21, 2006 IP