Search code examples
javascriptcookiescolorbox

How do change javascript cookie from 15 day count to manually do a new cookie?


I'm implementing a script with Colorbox which shows a nice popup the first time a user visits a website. This script that I found is setting the cookie to last for 15 days - what I want is to manually be able to manage the "cookie-update" by my own. In my case I want to use this script to show the user that my website has been updated and using the Colorbox to show the changelog; just on the first visit. Something like this:

  • Update #1 -> New cookie tells there's a new update -> Show Colorbox 1 time -> Then no more, until...
  • Update #2 -> New cookie tells there's a new update -> Show Colorbox -> ...and so on...

Short version of the question: How do I change the script from a 15 day timer to manually be able to change it so I can decide when I want to show my Colorbox with a new changelog?

The original script is located here: http://papermashup.com/automatic-jquery-site-subscription-lightbox/


Solution

  • You need to put a date in the site - Note JS months start at zero

    DEMO

    var latestUpdate=new Date(2012,11,10).getTime(); // 10th of December
    var whatVersionSeen = parseInt($.cookie("visited"));
    if (isNaN(whatVersionSeen)) whatVersionSeen = 0; // first time
    if (whatVersionSeen < latestUpdate) { // whatever in the cookie < latest update
      $.cookie('visited', latestUpdate, { expires: 365 });
      $.colorbox({width:"580px", inline:true, href:"#subscribe_popup"});
    }
    

    the above code replaces

    if (document.cookie.indexOf('visited=true') == -1) {
      var fifteenDays = 1000*60*60*24*15;
      var expires = new Date((new Date()).valueOf() + fifteenDays);
      document.cookie = "visited=true;expires=" + expires.toUTCString();
      $.colorbox({width:"580px", inline:true, href:"#subscribe_popup"});
     }
    

    completely and expects to find $.cookie