设置cookie过期时间

low*_*ase 0 javascript cookies jquery bootstrap-modal

我正在设置 cookie 以在页面加载时启动模态。目前,模态只发生一次。

我需要它每个月重置一次,以便每个访问者每个月都会弹出一次。

是否可以修改我的代码来实现这一点,还是我需要以另一种方式做到这一点?任何帮助表示赞赏。

$(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=/";
}
Run Code Online (Sandbox Code Playgroud)

});

emp*_*ric 5

只是不要在你的 cookie 中设置一个静态的到期日期,而是一个动态的:

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=/";
Run Code Online (Sandbox Code Playgroud)