javascript - JQuery Cookie.js 保存 cookie

标签 javascript jquery cookies

您好,我正在使用 jquery cookie.js 来存储 cookie。

我已经搜索了代码,并且使用 cookie.js 插件它工作正常, 但我遇到一个问题,当我刷新页面时,cookie 会自动保存并且弹出窗口不会显示。当任何用户进入该网站时,都会出现一个弹出窗口,用户必须单击“继续”按钮才能进入该网站,

我基本上不允许用户在单击“继续”按钮之前进入该网站。

这是我的 html 代码

<div id="popup-container">
<div class="wrapper">
<div id="popup-window">
  <div class="splash-bg">
    <h1>Are You Over 18? Click Continue if you are or return to previous 
      page</h1>
  <a type="button" class="close">Continue</a>
</div>
</div>

这是我正在使用的js代码

<script type="text/javascript">
     jQuery(document).ready(function(){  
      jQuery('#popup-container a.close').click(function(){
          jQuery('#popup-container').fadeOut();
          jQuery('#active-popup').fadeOut();
      });

      var visits = jQuery.cookie('visits') || 0;
      visits++;

      jQuery.cookie('visits', visits, { expires: 1, path: '/' });

      console.debug(jQuery.cookie('visits'));

      if ( jQuery.cookie('visits') > 1 ) {
        jQuery('#active-popup').hide();
        jQuery('#popup-container').hide();
      } else {
          var pageHeight = jQuery(document).height();
          jQuery('<div id="active-popup"></div>').insertBefore('body');
          jQuery('#active-popup').css("height", pageHeight);
          jQuery('#popup-container').show();
      }

      if (jQuery.cookie('noShowWelcome')) { jQuery('#popup-
      container').hide(); jQuery('#active-popup').hide(); }
    }); 

    jQuery(document).mouseup(function(e){
      var container = jQuery('#popup-container');

      if( !container.is(e.target)&& container.has(e.target).length === 0)
      {
        container.fadeOut();
        jQuery('#active-popup').fadeOut();
      }

    });
 </script>

最佳答案

当您有多次 cookie 访问时,cookie 可能不是 cookie 数组中的第一个 cookie,因此您必须尝试使用​​以下两种方式获取该 cookie

jQuery.cookie('visits')

jQuery.cookie(' visits')

关于javascript - JQuery Cookie.js 保存 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46806779/

相关文章:

javascript - 如何为新添加的行添加背景颜色angularjs

jquery - 折叠和展开的 DIV 问题

javascript - 使用 cookie 设置/获取输入值

http - 逗号是 cookie-value 中的有效字符吗

javascript - 简单的 NPM 包捆绑/销售?

javascript - 用于调整两个 div 大小的垂直拖动条

javascript - Swiper API Jquery

javascript - 检查表单中所有复选框的状态

ajax - 有没有办法在带有@ResponseBody 注释的方法中添加cookie?

javascript - Angularjs如何使用双向数据绑定(bind)保存数据