将cookie添加到现有脚本中


Add cookies to existing script

我已经在我的网站上添加了一些代码,但就是不知道如何添加cookie功能这是迄今为止的代码:

<script type="text/javascript">
$(document).ready(function () {
    $.fancybox('<p class="msgAlert">The following pictures contain nudity. <br />If you are not allowed or willing to watch such content please leave this page.<p><a href="javascript:void(0);" id="do_accept" class="button butgreen">Yes I Accept</a></p></p>', {
        closeBtn: false,
        closeClick: false,
        helpers: {
            overlay: {
                css: {
                    'background': 'rgba(0, 0, 0, 0.9)'
                }
            }
        }
    });
    $("body").on("click", "#do_accept", function () {
        $.fancybox.close();
    });
    $("body").on("click", ".fancybox-overlay", function () {
        window.location.href = "index.php";
    });
});
</script>

我不能添加php代码,所以我需要一些建议。这个想法是有一个10天的cookie,以便每10天只显示一次信息。链接如下:https://www.olafernst.com/photo/gallerynudes.html非常感谢

要在javascript中设置cookie,只需使用document.cookie

document.cookie = "Name=Value; expires=<DATE>';

若要获得正确格式的日期,可以在date对象上使用toUTCString方法。

要将天数添加到当前日期,您可以使用此问题的答案:如何将天数添加到今天';s日期?

您使用jquery,这样您就可以使用jquery cookie插件作为@gustavogb在注释中的写入。