【问题标题】:JQuery Cookie.js Save cokkieJQuery Cookie.js 保存cookie
【发布时间】:2017-10-18 17:25:32
【问题描述】:

您好,我正在使用 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>

【问题讨论】:

  • 不要检查&gt; 1。检查实际值,或者它是数字还是对象
  • 当有人刷新页面时,cookie 会自动保存,我正在寻找阻止这种情况发生的解决方案......
  • 我试过了,但是不行

标签: javascript jquery cookies


【解决方案1】:

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

jQuery.cookie('visits')

jQuery.cookie(' visits')

【讨论】:

  • console.log(document.cookie) 你能看到什么?
  • 我只是想要一些条件来通过刷新页面来停止自动保存,除非用户单击继续按钮...这是我获取代码的链接jqueryscript.net/lightbox/…
  • 嗨,你能解决你的 cookie 问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 2017-07-13
  • 2014-05-22
相关资源
最近更新 更多