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 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    94
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Changing a cookies expiry date

    Hello,

    I have the following code -

    Code:
      jQuery(document).ready(function(){
        if (document.cookie.indexOf('visited=true') == -1) {
            var fifteenDays = 1000*60*60*24*365;
            var expires = new Date((new Date()).valueOf() + fifteenDays);
            document.cookie = "visited=true;expires=" + expires.toUTCString();
            }
    });
    I'm using it for a pop-up on a page on my website, giving users a reminder to register, and setting it to expire in 365 days.

    However, within the pop-up I need some text saying "Remind Me Later" that will instead expire the cookie in 24 hours, so they get the pop-up again the next day.

    Is this possible? Any help appreciated.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Yes, just re-write the cookie with the new expiry date.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Nov 2010
    Posts
    94
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Yes, just re-write the cookie with the new expiry date.
    Would something like this work then?

    Code:
    jQuery(document).ready(function(){
        if (document.cookie.indexOf('visited=true') == -1) {
            var fifteenDays = 1000*60*60*24*365;
            var expires = new Date((new Date()).valueOf() +  fifteenDays);
            document.cookie = "visited=true;expires=" +  expires.toUTCString();
            }
    
    $("#some-text").click(function() {
        if (document.cookie.indexOf('visited=true') == -1) {
            var fifteenDays = 1000*60*60*24*1;
            var expires = new Date((new Date()).valueOf() +  fifteenDays);
            document.cookie = "visited=true;expires=" +  expires.toUTCString();
            }
    });
    });

  • #4
    New Coder
    Join Date
    Nov 2010
    Posts
    94
    Thanks
    23
    Thanked 0 Times in 0 Posts
    That didn't work at all, but it shows what I'm trying to do if someone could help further. Thanks.

  • #5
    New Coder
    Join Date
    Nov 2010
    Posts
    94
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Still haven't managed to resolve this if anyone could please help.

  • #6
    New Coder
    Join Date
    Jan 2011
    Posts
    52
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by swiltch View Post
    Still haven't managed to resolve this if anyone could please help.
    If you're using jQuery, why don't you use its built-in cookie handling?

    Here's as good an explanation as any: http://www.electrictoolbox.com/jquery-cookies/


  •  

    Posting Permissions

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