Solved

Changing cookie expiration

Posted on 2011-02-17
12
569 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 250 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

724 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