【问题标题】:How to set cookies foe notification bar如何为通知栏设置cookies
【发布时间】:2015-01-02 21:32:30
【问题描述】:

我有一个通知栏的代码,我需要,当用户第一次运行页面时,它会显示通知栏,当用户刷新页面时它不会显示。

这是我使用的代码,

<div id="page-wrap">

    <div id="note">
        You smell good. <a id="close">[close]</a>
    </div>

    <h1>Pop From Top Message</h1>

    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis </p>

    </div>

jquery:

 <script>
   close = document.getElementById("close");
   close.addEventListener('click', function() {
     note = document.getElementById("note");
     note.style.display = 'none';
   }, false);
  </script>

这里是 jsfiddle:http://jsfiddle.net/t90096vs/

如何添加 cookie 来解决这个问题。

谁能帮助我?提前致谢。

【问题讨论】:

  • 谁能帮帮我?
  • 您一年前提出的问题stackoverflow.com/questions/18904697/… 展示了如何测试是否是第一次访问网站。为什么不能使用相同的技术?
  • 那是一年多以前的事了,你不应该更有经验吗?你需要多长时间才能学会编程?
  • 对不起,我没仔细看。

标签: jquery cookies


【解决方案1】:
$(function() {
    function GetCookie(name) {
        var arg=name+"=";
        var alen=arg.length;
        var clen=document.cookie.length;
        var i=0;

        while (i<clen) {
            var j=i+alen;
                if (document.cookie.substring(i,j)==arg)
                    return "here";
                i=document.cookie.indexOf(" ",i)+1;
                if (i==0) 
                    break;
        }

        return null;
    }
    var visit = GetCookie("showed_note");
    if (visit == null) {
        $("#note").show();
        document.cookie = "showed_note=true";
    } else {
        $("#note").hide();
    }
});

DEMO

【讨论】:

  • 请问,我可以在哪里添加?
  • 把它放在你的&lt;script&gt; 块中。
  • 我已经添加了你的代码..但仍然保持不变..我已经检查了添加 index.html 和 modernizr.custom.80028.js。
  • #notification更改为通知栏的实际ID。我只是把它作为占位符。
  • 还是不行@barmar。你看看我的jsfiddle吗?谢谢
猜你喜欢
  • 2013-12-26
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 2014-09-20
  • 2022-10-01
  • 1970-01-01
相关资源
最近更新 更多