SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    show/hide with cookies

    Hi,

    when i open news and refresh everything good, bu when i closed news and resfresh still show open news.

    I try delete cookies with code: function del_cookie(name) { document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;'; }, but didin't work.


    Code JavaScript:
    <script type="text/javascript">
    function createCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }
     
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }
    function del_cookie(name)
     { document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;'; }
     
    function change(id){
         ID = document.getElementById(id);
     
         if(ID.style.display == "")
         {
              ID.style.display = "none";
              del_cookie("id");
         }
         else
              ID.style.display = "";
              createCookie("id",id,7); /* 1 week */
          }
     
    window.onload = function() {
        try{change(readCookie("id"));}catch(e){} //rough example
    };
    </script>
    <div>
    <div onclick="change(5)" style="cursor: hand">
        <a href = "#">+</a>News
    </div>
    <div style="display: none" id="5">
        News1<br/>
        News2<br/>
    </div>
    <div onclick="change(4)" style="cursor: hand">
        <a href = "#">+</a>News2
    </div>
    <div style="display: none" id="4">
        News2 1<br/>
        News2 2<br/>
    </div>
    </div>

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lolalola View Post
    Hi,

    when i open news and refresh everything good, bu when i closed news and resfresh still show open news.

    I try delete cookies with code: function del_cookie(name) { document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;'; }, but didin't work.

    Your createCookie function is hard coded to set the path to '/', so you must specify the same path when deleting.
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •