?
Solved

Changing cookie expiration

Posted on 2011-02-17
12
Medium Priority
?
571 Views
Last Modified: 2012-05-11
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
      }
0
Comment
Question by:TCCIRM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34915532
get the old value first (getCookie(c_name)) and recreate (setCookie:function(name, value, days)) it with the new expiration date :)
0
 
LVL 3

Accepted Solution

by:
imaki06 earned 500 total points
ID: 34915540
You should find the call to that setCookie-function.

There is three parameters name, value and amount of days. Change the last parameter to 360, and thats it..

Like this:
setCookie("name","cookie value", 360);
0
 

Author Comment

by:TCCIRM
ID: 34915584
So modification to this string will not do the trick?

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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
ID: 34915606
setCookie(myCookieName, getCookie(myCookieName), 360);

Open in new window

0
 

Author Comment

by:TCCIRM
ID: 34915727
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
      }
}
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34915740
>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
0
 

Author Comment

by:TCCIRM
ID: 34916268
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...
0
 

Author Comment

by:TCCIRM
ID: 34916300
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
0
 

Author Comment

by:TCCIRM
ID: 34917355
Still plugging away...  still not able to make the leap...  
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34935404
If you place alert/console.log as first command of setCookie and getCookie do you get/see them ?
0
 

Author Comment

by:TCCIRM
ID: 34950914
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....
0
 

Author Closing Comment

by:TCCIRM
ID: 34980277
Answers were not specfic to my problem.  Moving on...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question