Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    Update a cookie value without changing its expire date?

    Hi:
    Just wondering, lets say I set a cookie with an expiration date, such as:

    Code:
    document.cookie = "mycookie="+cookievalues+";path=/;expires=" + expireDate.toGMTString()
    Now, if i wanted to update the above cookie's value without changing its expire date, am I correct that I should simply set the cookie again with the new value, but without the "expires" part at the end? For example:

    Code:
    document.cookie = "mycookie="+newcookievalues+";path=/"
    Will this work in updating the cookie but still have it expire on the original date?

    Thanks,
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    You still need the expires part otherwise your cookie will be deleted.

    Try this example

    PHP Code:
    <script type="text/javascript">
    <!--
    expireDate=new Date("dec 25,2005 00:00:00");

    function 
    test1(){
    cookievalues="Hello World"
    document.cookie "mycookie="+escape(cookievalues)+";path=/;expires=" expireDate
    }

    function 
    test2(){
    cookievalues="New Cookie Value"
    document.cookie "mycookie="+escape(cookievalues)+";path=/;expires=" expireDate
    }

    function 
    test3(){
    cookievalues="Help me, I'm melting"
    document.cookie "mycookie="+escape(cookievalues)+";path=/"
    }


    // -->
    </script> 

    <a href="#null" onclick="test1()">Update Cookie 1</a> 
    <a href="#null" onclick="test2()">Update Cookie 2</a>  
    <a href="#null" onclick="test3()">Update Cookie 3</a> 
    When you click Update Cookie 3 the persistant cookie is deleted
    Last edited by Mr J; 08-30-2005 at 06:41 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Hi MJ:
    Thanks for the reply. So there's no easy way to update the value of a permanent cookie without knowing the original expiration date of the cookie? In your example below, var expireDate contains the expire original date. What if I don't know the expiration date, or my script is set up so this date isn't stored in a variable? I was hoping there's just a way to update the cookie value, and leave the expiration date of the cookie in question "as is."
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Hi George.

    So the senario is, if the expiry date was dynamically created using the current date then you would not know the expiry date, hmm.

    To my knowledge, which I always class as limited, you cannot access the expiry date in the manner that you want but,

    what if you had the expiry date saved to the cookie then when the cookie has been updated have the cookie resaved with that date again?
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Quote Originally Posted by Mr J
    Hi George.
    So the senario is, if the expiry date was dynamically created using the current date then you would not know the expiry date, hmm.
    Yep, that's the problem here.
    what if you had the expiry date saved to the cookie then when the cookie has been updated have the cookie resaved with that date again?
    That would be my last resort, but I figured there must be an easier, more elegant way. Originally I thought just by setting a cookie with the same name as an existing one, it overwrites the later with the new value, keeping intact what's not changed, in this case, the expiration date.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    George.

    I'll keep my eyes open for a more elegant way, if you do come across a solution to reference the expiry date please post it.

    Thanks

    Jeff
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #7
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The expires property is write only...
    What you can do is send the expires date as a key/value pair when you set the cookie and then check for the cookie name and split out that key/value pair to use as the new(old) expires date...

    Here is a quick example...

    Code:
    <script type="text/javascript"> 
      function setCookie(cookieValue){
       var theDate  = new Date(new Date().setTime(new Date().getTime()+30*24*60*60*1000)); // 30 days
        if(/myCookie/.test(document.cookie)){
           theDate = unescape(document.cookie).split('expireDate=')[1]; // split out date to reuse
        }  document.cookie = 'myCookie='+cookieValue+',expireDate='+theDate+';expires='+theDate;
      } 
    </script> 
    </HEAD> 
    
    <BODY>
    <a href="#null" onclick="setCookie('Set Cookie');return false">Set Cookie</a> 
    <a href="#null" onclick="setCookie('Update Cookie');return false">Update Cookie</a>
    <a href="#null" onclick="alert(document.cookie);return false">Alert Cookie</a>
    Elsewise, you can try using the date as the cookie name (08/30/2005) and use a regular expression to check for the cookie name and pass that as the expires date when you reset the cookie...

    .....Willy
    Last edited by Willy Duitt; 08-30-2005 at 11:50 PM.

  • #8
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Thanks for the code Willy. I see you're storing the original expiration date explicitly via "expireDate" to access it. I guess there really is no shortcut way to update a cookie value without having to set the expiration date again (using the original date somehow) hehe.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •