始终显示带有cookie的信息框

Information box with cookie will be displayed all the time

本文关键字:信息 cookie 显示      更新时间:2023-09-26

这就是为什么当我点击我的按钮,所以重新加载页面,我可以记住看起来像框"走开了,当我点击。但是当我点击的时候,它没有设置cookie,它也没有检查是否设置?

所以现在的问题是:

  • Cookie不会添加
  • 不检查我们今天的日期比我们有cookie的日期多10天换句话说,在显示内容之前再走10天。

我在asp.net中使用web表单

Html代码

<div style="position: relative;" id="#CookieBox">
            <div class="alert alert-info fade in nomargin" style="margin: 0; position:fixed; bottom:0; right:0; left:0; height:65px; border-radius:0; z-index:10000;">
                <div class="container">
                    <p style="font-size:11px;">
                        Cookies er nødvendige for at få hjemmesiden til at fungere, men cookies giver også info om hvordan du bruger vores hjemmeside. og Cookies på denne hjemmeside bruges primært til trafikmåling
                        <button class="btn btn-info" id="AccepterCookie">Accepter</button>
                        <a href="../cookie/Cookies.pdf" target="_blank">Læs mere</a>
                    </p>
                </div>
            </div>
        </div>

Jquery代码在这里

<script>
            var datoCookieValue = (new Date).setDate(10); //This cookie number of days
            //It must check in sf whether it is null or whether it is more than the 10 days that I have chosen.
            if (datoCookieValue == null || datoCookieValue >= (new Date).getDay)//checking up on whether the cookie is "set"
            {
                //when you click "Accept" so be content with cookie box contents go away
                $("#AccepterCookie").click(function () {
                    $("#CookieBox").slideDown("slow");//shuts down the cookie box.
                    //addcookie here
                    $.cookie("CookieAdd", 1, { expires: datoCookieValue });
                });
            }
            else
            {
                //must first show box on the 10 days it will look when the 
                //10 days are up then the display contents again.
                $("CookieBox").hide("fast");
            }
        </script>

在jquery中有时使用#CookieBox和CookieBox选择器。如果你检查日期的条件在我看来是不必要的,因为cookie有一个到期时间。

这是我的解决方案:

<div style="position: relative; display:none" id="CookieBox">
            <div class="alert alert-info fade in nomargin" style="margin: 0; position:fixed; bottom:0; right:0; left:0; height:65px; border-radius:0; z-index:10000;">
                <div class="container">
                    <p style="font-size:11px;">
                        Cookies er nødvendige for at få hjemmesiden til at fungere, men cookies giver også info om hvordan du bruger vores hjemmeside. og Cookies på denne hjemmeside bruges primært til trafikmåling
                        <button class="btn btn-info" id="AccepterCookie">Accepter</button>
                        <a href="../cookie/Cookies.pdf" target="_blank">Læs mere</a>
                    </p>
                </div>
            </div>
        </div>
jQuery:

var datoCookieValue = $.cookie('CookieAdd');
  if (!datoCookieValue) {
      $("#CookieBox").show();
      $("#AccepterCookie").click(function() {
          $("#CookieBox").hide("slow");
          $.cookie("CookieAdd", 1, {
              expires: 10
          });
      });
  }

JS Fiddle: https://jsfiddle.net/cLtazsry/