Javascript Cookie不适用于所有页面 [英] Javascript cookie doesn't work on all page

查看:86
本文介绍了Javascript Cookie不适用于所有页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实时网站- http://chitrchatr.com

我在退出按钮&上成功添加了setCookie函数.另一个链接.因此,如果有人关闭弹出窗口或单击该链接,则cookie将被存储&;弹出窗口永远不会出现在他/她身上.

I successfully add setCookie function on exit button & another link. So, if anyone close popup or click on that link then cookie will be stored & popup never appears for him/her.

它工作正常,我关闭/转到链接它,它存储cookie,但是当我尝试转到网站的另一个页面/链接时,它又出现了(cookie已经存储,但它消失了).关于如何解决它的任何想法,因此如果有人在存储cookie后转到另一个页面,它将永远不会出现.

It works perfectly, i close/go to link it, it store cookie but when i try to go another page/link of website it appears again(cookie is already stored but it appers). Any idea how to fix it so it will never appears if anyone go to another page after storing cookie.

我的HTML-

<div id="popupBox">
 <div id='popupContent' class='visiblebox' style='width:500px;height:446px;z-index:999999;left: 31%; top: 15%;'>
 <a onClick="document.getElementById('popupBox').style.display='none'; setCookie('abc', 'def', 1)" href='#' id='closebox' title='Close this box'>X</a>
.... 
PopUp content here
....
<a onClick="document.getElementById('popupBox').style.display='none'; setCookie('abc', 'def', 1)" href="http://chitrchatr.com/signup-and-win-new-smartphones-and-get-our-service-for-free/" target="_blank"><img class="alignnone size-full wp-image-1548" alt="Promotion_06" src="http://chitrchatr.com/wp-content/uploads/2014/01/Promotion_06.png" width="243" height="61" /></a>
 </div>
</div>

Javascript-

Javascript-

    <script type="text/javascript">

        function setCookie(c_name,value,exdays)
        {
        var exdate=new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
        document.cookie=c_name + "=" + c_value;
        }

        function getCookie(c_name)
        {
        var c_value = document.cookie;
        var c_start = c_value.indexOf(" " + c_name + "=");
        if (c_start == -1)
          {
          c_start = c_value.indexOf(c_name + "=");
          }
        if (c_start == -1)
          {
          c_value = null;
          }
        else
          {
          c_start = c_value.indexOf("=", c_start) + 1;
          var c_end = c_value.indexOf(";", c_start);
          if (c_end == -1)
          {
        c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start,c_end));
        }
        return c_value;
        }


        if(getCookie('abc')=="def" && document.getElementById('popupBox'))
        document.getElementById('popupBox').style.display='none';
</script>

推荐答案

将路径添加到您的cookie

add path to your cookie

function setCookie(name, value, days, secure) {
  var expires = '';
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    expires = '; expires='+date.toGMTString();
  }
  var domain = locDomain;
  document.cookie = name + '='+escape(value) + expires + '; path=/' + (domain ? '; domain=.' + domain : '') + ((secure && locProtocol == 'https:') ? '; secure' : '');
}

这篇关于Javascript Cookie不适用于所有页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆