Link to home
Start Free TrialLog in
Avatar of TCCIRM
TCCIRMFlag for United States of America

asked on

Changing cookie expiration

I am running a script that currently sets a cookie that expires at the end of the session when the browser close's.  I would like to set the expiration date for 360 days.  Not sure how to make this change in the following code:

      setCookie:function(name, value, days){
         var expirestr=''
         if (typeof days!="undefined") //if set persistent cookie
            expirestr="; expires="+expireDate.setDate(new Date().getDate()+days).toGMTString()
        document.cookie = name+"="+value+"; path=/"+expirestr
      }
Avatar of leakim971
leakim971
Flag of Guadeloupe image

get the old value first (getCookie(c_name)) and recreate (setCookie:function(name, value, days)) it with the new expiration date :)
ASKER CERTIFIED SOLUTION
Avatar of imaki06
imaki06

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TCCIRM

ASKER

So modification to this string will not do the trick?

expirestr="; expires="+expireDate.setDate(new Date().getDate()+days).toGMTString()

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TCCIRM

ASKER

Okay I'm following you somewhat...  I'm deconstucting this to learn.  I think I should have included the entire routine:  This is not as clear cut as I thought it was.  The cookies name is fluidtextresizer.   So by calling the old value (if it is available) first...  then set the expiration date?

fluidtextresizer.routines={
      getCookie:function(Name){
      var re=new RegExp(Name+"=[^;]*", "i");   //construct RE to search for target name/value pair
      return (document.cookie.match(re))? document.cookie.match(re)[0].split("=")[1] : null //return cookie value if found or null
      },
      
      setCookie:function(name, value, days){  
      var expirestr=''
      if (typeof days!="undefined") //if set persistent cookie
      expirestr="; expires="+expireDate.setDate(new Date().getDate()+days).toGMTString()  
      document.cookie = name+"="+value+"; path=/"+expirestr
      }
}
>The cookies name is fluidtextresizer.   So by calling the old value (if it is available) first...  then set the expiration date?

yes, if it not exist you're just creating a cookie with a null value
Avatar of TCCIRM

ASKER

Yeah I'm not getting it...   Can't seem to adapt your recommendations into the code I have and get it to work....  The principle makes sense, application does not...
Avatar of TCCIRM

ASKER

I guess the crazy part is I can get the simple application to work but this script is a little more complicated.  Complete script attached... fluidtextresizer.js
Avatar of TCCIRM

ASKER

Still plugging away...  still not able to make the leap...  
If you place alert/console.log as first command of setCookie and getCookie do you get/see them ?
Avatar of TCCIRM

ASKER

As you tell by now, I'm not real adept with JavaScript...  I tried the following but it throws and error:

alert( document.cookie("fluidtextresizer") );

Using FF cookie viewer I am able to view the values and can see whether my modifications are working.  I've been able to create a simple cookie separate from the script I attached. I am forced to use this script but have not been able to make the leap....
Avatar of TCCIRM

ASKER

Answers were not specfic to my problem.  Moving on...