Results 1 to 4 of 4

Thread: Cookies

  1. #1
    Join Date
    Feb 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cookies

    I have this:

    Code:
    <head>
    <script language="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 eraseCookie(name) {
      createCookie(name,"",-1);
    }
    
    function hidemembers() {
      toggleBox('members','none');
      createCookie('members','none');
    }
    
    function showmembers() {
      toggleBox('members','block');
      createCookie('members','block');
    }
    
    function toggleBox(id, display) {
      // DOM3 = IE5, NS6
      if (document.getElementById) {
        document.getElementById(id).style.display = display;
      // Netscape 4
      } else if (document.layers) {
        document.layers[id].display = display;
      // IE 4
      } else if (document.all) { 
        document.all[id].style.display = display;
      }
    }
    
    window.onload = function() {
      var memberDisplay = readCookie('members');
      if (memberDisplay !== null) {
        toggleBox('members',memberDisplay);
      }
    };
    //-->
    </script>
    
    </head>
    
    <body>
    <a href="javascript:showmembers()">Show</a> | <a href="javascript:hidemembers()">hide</a>
    
    <div id="members">Content here</div>
    </body>
    However i need to use the same code for different DIV's. I want to do this by using the code on the same page over and over again, but how can i change it so that it works more than once on a page?

    (I am changing the div name, for example document.all.members3.style.display in the code correctly, the problem is the with the cookie.)
    Last edited by Tabo; 04-01-2007 at 10:55 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <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 eraseCookie(name) {
      createCookie(name,"",-1);
    }
    
    function hidemembers(id) {
      toggleBox(id,'none');
      createCookie(id,'none');
    }
    
    function showmembers(id) {
      toggleBox(id,'block');
      createCookie(id,'block');
    }
    
    function toggleBox(id, display) {
      // DOM3 = IE5, NS6
      if (document.getElementById) {
        document.getElementById(id).style.display = display;
      // Netscape 4
      } else if (document.layers) {
        document.layers[id].display = display;
      // IE 4
      } else if (document.all) { 
        document.all[id].style.display = display;
      }
    }
    
    if(document.getElementsByTagName)
    window.onload = function() {
    var divs=document.getElementsByTagName('div')
    for (var i_tem = 0; i_tem < divs.length; i_tem++)
    if(divs[i_tem].id!='' && readCookie(divs[i_tem].id))
        toggleBox(divs[i_tem].id,readCookie(divs[i_tem].id));
    }
    
    </script>
    
    </head>
    
    <body>
    <a href="javascript:showmembers('members')">Show</a> | <a href="javascript:hidemembers('members')">hide</a><br>
    
    <div id="members">Content here</div>
    <a href="javascript:showmembers('blah')">Show</a> | <a href="javascript:hidemembers('blah')">hide</a>
    
    <div id="blah">Content here</div>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks. This works on a page on its own, but when i put it on an existing page and it doesn't work :S need to look for clashing code.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Most likely it is an onload conflict. It could be other things.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •