将cookie设置为在div上过期

时间:2017-02-13 15:11:14

标签: jquery html css

我在使用bootstrap navbar-inverse的页面上有一个div。当页面加载时,用户将单击关闭按钮,然后将cookie设置为在2天后过期。因此,下次用户访问该页面并且cookie过期时,div将被隐藏。否则显示div。感谢您的回复。

//这里是div,页面上还有其他内容。

<div id="myFooter">
  <div class="navbar-inverse1 navbar-fixed-bottom" role="navigation">
    <div class="container">
      <div class="navbar-text pull-right">
        <button type="button" class="close" id="myButton" data-dismiss="navbar">&times;</button>
        <h1 class="text-center">Do Something Here</h1>
        <p>Lorem ipsum is a pseudo-Latin text used in web design, typography, layout, and printing in place of English to emphasise design elements over content. It's also called placeholder (or filler) text. It's a convenient tool for mock-ups. It helps to outline the visual elements of a document or presentation, so to deliberately render its content nonsensical; it's not genuine, correct, or comprehensible Latin anymore. While lorem ipsum's still resembles classical Latin</p>
      </div>
    </div>
  </div>
</div>

//这是脚本,但不能让它工作。

<script>
      $(document).ready(function() {
    // initially popup click to hide:
       $('.close').click(function() {
          $('#myFooter').hide();
        });
    // Check for the "whenToShowPopup" cookie, if not found then show the popup and save the cookie.
    // The cookie will expire and every 2 days and the popup will show again.
        limit = 5;
        idleTime = 0;
        if ($.cookie('whenToShowPopup') == null) {
          function timerIncrement() {
            idleTime = idleTime + 1;
            if (idleTime > $limit && $.cookie('whenToShowPopup') != 'yes') {
              $('#myFooter').show();
              idleTime = 0;
              console.log('whenToShowPopup', 'idleTime');
          // Create expiring cookie, 2 days from now:
              $.cookie('whenToShowPopup', 'yes', {
                expires: 2,
                path: '/'
              });
          // Show Popup
              $('#myFooter').hide();
              console.log('whenToShowPopup', 'cookie created');
            };
          });
      }); 
      </script>

1 个答案:

答案 0 :(得分:0)

你的javascript似乎缺少一些花括号。

这是格式化的代码。 (可能无法解决你的逻辑)。

<script>
$(document).ready(function() {
  // initially popup click to hide:
  $('.close').click(function() {
    $('#myFooter').hide();
  });
  // Check for the "whenToShowPopup" cookie, if not found then show the popup and save the cookie.
  // The cookie will expire and every 2 days and the popup will show again.
  limit = 5;
  idleTime = 0;
  if ($.cookie('whenToShowPopup') == null) {
    function timerIncrement() {
      idleTime = idleTime + 1;
      if (idleTime > $limit && $.cookie('whenToShowPopup') != 'yes') {
        $('#myFooter').show();
        idleTime = 0;
        console.log('whenToShowPopup', 'idleTime');
        // Create expiring cookie, 2 days from now:
        $.cookie('whenToShowPopup', 'yes', {
          expires: 2,
          path: '/'
        });
        // Show Popup
        $('#myFooter').hide();
        console.log('whenToShowPopup', 'cookie created');
      };
    }
  }
});
</script>