【问题标题】:JS cookie bar would not repeat on the refreshJS cookie bar 不会在刷新时重复
【发布时间】:2022-01-29 16:52:47
【问题描述】:

得到这个 cookie 条:

HTML:

<div id="cookie-bar-bottom" class="cookie-bar">text...
<a href="index-cookies.html" class="cookie-policy">
   <b><text style="color: rgb(230,0,100)">Cookies</text></b>
</a>
<input id="cookie-hide" class="cookie-hide" onclick="this.parentNode.parentNode.style.display = 'none'" value="Agree" type="button">
</div>

CSS:

.cookie-bar {
    position: fixed;
    width: 100%;
    bottom: 0;
    right: 0;
    left: 0;
    height: 30px;
    text-align: center;
    line-height: 25px;
    background: rgb(0,140,250,1);
    color: white;
    font-size: 17px;
    font-weight: 100;
    transition: .8s;
    animation: slideIn .8s;
}

我可能需要更好的解决方案,可能通过 JS 来防止下次刷新时弹出 cookie 栏。理想情况下,浏览器缓存会记住此 cookie 已被同意,因此不会再出现在该浏览器中。

有人帮我吗? 谢谢。

【问题讨论】:

    标签: javascript cookies


    【解决方案1】:

    这是唯一缺少的东西(JS 功能):

    <script>
    function closeCookieNotice() {
        const nowDate = new Date();
        const expireDate = new Date(nowDate.setDate(nowDate.getDate() + 30)).toUTCString();
        document.cookie = "cookie_notice=0;path=/;expires=" + expireDate + ";";
        document.getElementById("cookie-bar-bottom").style.display = "none";
    };
    document.addEventListener("DOMContentLoaded", function() {
        const cookie_notice = ('; ' + document.cookie).split('; cookie_notice=').pop().split(';')[0];
        if (cookie_notice !== "0") {
            document.getElementById("cookie-bar-bottom").style.display = "block";
        }
    });
    </script>
    

    说明:它在栏关闭时设置 cookie 并检查 cookie 是否在页面加载时设置。如果设置了 cookie,则该栏不显示。简单、轻量级并且可以胜任。

    您可以在此处获取更多 cookie 通知样式/主题https://cookienotice.js.org/themes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多