Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-12-2012, 11:34 AM   PM User | #1
swiltch
New Coder

 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 0 Times in 0 Posts
swiltch is an unknown quantity at this point
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.
swiltch is offline   Reply With Quote
Old 09-12-2012, 12:25 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 09-12-2012, 12:50 PM   PM User | #3
swiltch
New Coder

 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 0 Times in 0 Posts
swiltch is an unknown quantity at this point
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();
        }
});
});
swiltch is offline   Reply With Quote
Old 09-12-2012, 02:40 PM   PM User | #4
swiltch
New Coder

 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 0 Times in 0 Posts
swiltch is an unknown quantity at this point
That didn't work at all, but it shows what I'm trying to do if someone could help further. Thanks.
swiltch is offline   Reply With Quote
Old 09-13-2012, 09:05 AM   PM User | #5
swiltch
New Coder

 
Join Date: Nov 2010
Posts: 51
Thanks: 8
Thanked 0 Times in 0 Posts
swiltch is an unknown quantity at this point
Still haven't managed to resolve this if anyone could please help.
swiltch is offline   Reply With Quote
Old 09-13-2012, 01:06 PM   PM User | #6
Calvert Tripesi
New Coder

 
Join Date: Jan 2011
Posts: 51
Thanks: 0
Thanked 9 Times in 9 Posts
Calvert Tripesi is an unknown quantity at this point
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/
Calvert Tripesi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:51 AM.


Advertisement
Log in to turn off these ads.