如果接受则不会再次显示 Cookie 同意弹出窗口

发布于 2025-01-09 14:41:41 字数 685 浏览 3 评论 0原文

假设用户单击 cookie 同意弹出窗口中的“接受”链接,则该弹出窗口不应在 24 小时内再次显示。有人可以告诉我如何完成这件事吗?

<div id="cookieConsent">
  <div id="closeCookieConsent">x</div>
  This website is using cookies. <a href="cookies-policy" target="_blank">More info</a>. <a class="cookieConsentOK"><b>ACCEPT</b></a>
</div>
$(document).ready(function() {
  setTimeout(function() {
    $("#cookieConsent").fadeIn(200);
  }, 4000);
  
  $("#closeCookieConsent, .cookieConsentOK").click(function() {
    $("#cookieConsent").fadeOut(200);
  });
});

Supposing the user clicks the 'ACCEPT' link in the cookie consent popup, then the popup should not show again for 24hrs. Can someone show how me how to get this done?

<div id="cookieConsent">
  <div id="closeCookieConsent">x</div>
  This website is using cookies. <a href="cookies-policy" target="_blank">More info</a>. <a class="cookieConsentOK"><b>ACCEPT</b></a>
</div>
$(document).ready(function() {
  setTimeout(function() {
    $("#cookieConsent").fadeIn(200);
  }, 4000);
  
  $("#closeCookieConsent, .cookieConsentOK").click(function() {
    $("#cookieConsent").fadeOut(200);
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2025-01-16 14:41:41

我对您的代码进行了一些更改,以存储一个值为 true 的本地变量,并且从用户单击“接受”时起,过期时间为 10 秒。将此部分更改为如下内容:

now.getTime() + (1000 * 60 * 60 * 24)

每次加载页面时,脚本都会查找此存储的变量,以及过期时间是否大于存储的值然后删除存储的变量并再次显示 cookie 接受消息。

这里是代码:

<div id="cookieConsent" style="visibility: hidden;">
    <div id="closeCookieConsent">x</div>
    This website is using cookies. <a href="cookies-policy" target="_blank">More info</a>. <a class="cookieConsentOK"><b>ACCEPT</b></a>
</div>

<script type="text/javascript">
    $(document).ready(function(){   

        var consentStr = localStorage.getItem('cookieSeen');

        if(!consentStr) {
            $("#cookieConsent").css('visibility','visible').hide().fadeIn(200);
        } else {
            var consent = JSON.parse(consentStr);
            const now = new Date();

            if (now.getTime() > consent.expiry) {
                localStorage.removeItem('cookieSeen');
                return null;
            }           

            //console.log('Cookie accepted');
        }

        $("#closeCookieConsent, .cookieConsentOK").click(function() {
            const now = new Date();
            const item = {
                value: true,
                expiry: now.getTime() + 10000,
            };
            localStorage.setItem('cookieSeen', JSON.stringify(item) );
            $("#cookieConsent").fadeOut(200);
        }); 
    }); 
</script>

I have changed a little bit your code to store a local var with a value of true and expiration time of 10 seconds from the moment when user click on ACCEPT. Change this part to something like:

now.getTime() + (1000 * 60 * 60 * 24)

Every time page is loaded the script look for this stored var and if the expiration time is greater than the stored value then delete the stored var and show the cookie acceptance message again.

Here the code:

<div id="cookieConsent" style="visibility: hidden;">
    <div id="closeCookieConsent">x</div>
    This website is using cookies. <a href="cookies-policy" target="_blank">More info</a>. <a class="cookieConsentOK"><b>ACCEPT</b></a>
</div>

<script type="text/javascript">
    $(document).ready(function(){   

        var consentStr = localStorage.getItem('cookieSeen');

        if(!consentStr) {
            $("#cookieConsent").css('visibility','visible').hide().fadeIn(200);
        } else {
            var consent = JSON.parse(consentStr);
            const now = new Date();

            if (now.getTime() > consent.expiry) {
                localStorage.removeItem('cookieSeen');
                return null;
            }           

            //console.log('Cookie accepted');
        }

        $("#closeCookieConsent, .cookieConsentOK").click(function() {
            const now = new Date();
            const item = {
                value: true,
                expiry: now.getTime() + 10000,
            };
            localStorage.setItem('cookieSeen', JSON.stringify(item) );
            $("#cookieConsent").fadeOut(200);
        }); 
    }); 
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文