0

I am setting a cookie to launch a modal on page load. Currently, the modal only happens once.

I need it to reset every month, so that it keeps popping up once a month for every visitor.

Is it possible to modify my code to make this happen or do I need to do it another way? Any help appreciated.

$(document).ready(function () {
//if cookie hasn't been set...
if (document.cookie.indexOf("ModalShown=true")<0) {
    $("#newsletterModal").modal("show");
    //Modal has been shown, now set a cookie so it never comes back
    $("#newsletterModalClose").click(function () {
        $("#newsletterModal").modal("hide");
    });
    document.cookie = "ModalShown=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";
}

});

lowercase
  • 1,198
  • 9
  • 34
  • 56

3 Answers3

5

Just don't set a static expiration date in your cookie but a dynamic one:

var now = new Date(); //get the current date
now.setMonth(now.getMonth() + 1); //add one month to it
document.cookie = "ModalShown=true; expires=" + now.toUTCString() + "; path=/";
empiric
  • 7,825
  • 7
  • 37
  • 48
2

Hey I'm going to give simple code for setting remove and add cookie which might help you

function setCookie(name, value, expire) {
    document.cookie = name + "=" + escape(value) + ((expire === null) ? "" : ("; expires=" + expire.toGMTString()));
}

function checkCookie(name) {
    if (document.cookie !== "") {
        var toCookie = document.cookie.split("; ");
        for (var i = 0; i < toCookie.length; i++) {
            var CookieName = toCookie[i].split("=")[0];
            var CookieValue = toCookie[i].split("=")[1];
            if (CookieName === name) return unescape(CookieValue);
        }

    }
}

function removeCookie() {
    var CookieAlert;
    CookieAlert = document.getElementById('cookie');
    var expire = new Date();
    expire.setMonth(expire.getMonth() + 1);
    setCookie('agreeCookies', 'yes', expire);
}

window.onload = (function () {
    //IF cookie exists do something 
    if (checkCookie('agreeCookies') !== 'yes') {
        //Do something in your case dont start modal 
    } else {
        //START MODAL
    }
});

So in html you can use onclick="removeCookie()" on your close button in modal, basically you show modal and on close modal you set cookie, then after month when cookie expires you show it once again

Szymon
  • 1,281
  • 1
  • 20
  • 36
  • 1
    be careful using [`toGMTString()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/toGMTString) this function has been deprecated: `Deprecated This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.` – empiric Aug 18 '16 at 18:53
  • oh good to know thank you @empiric :) – Szymon Aug 18 '16 at 19:11
0

All you need to do is update the expiration date to be now + 1 month rather than 'Fri, 31 Dec 9999 23:59:59 GMT', but do it outside of the check for the cookie to ensure existing users with the old expiry date get an updated cookie.

Richard Scarrott
  • 6,638
  • 1
  • 35
  • 46