Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2003
    Thanked 0 Times in 0 Posts

    Site wide cookie setting and retrieval

    Hello, I hope someone can help me out.

    I am battling with a little javascript snippet that I picked up somewhere else. Anyway, at the moment all it does is hide or show an inline element.

    The javascript code:

    function toggle( targetId ){
      if (document.getElementById){
      		target = document.getElementById( targetId );
      			if (target.style.display == "none"){
      				target.style.display = "";
      			} else {
      				target.style.display = "none";
    Then I have an inline element, for instance. In this example it is shown by default.

    <div id="contributors" style="display:"";"> 
    Content to hide or show here
    To toggle back and forth between the two I've used the following:

    <a href="#" onclick="toggle('contributors');return false;">Toggle</a>
    Anyway, to the question. I am using this function on two seperate inline elements across an entire site, and I want the browser to remember the settings from the previous page. I am not quite sure, but I think this is possible with cookie management, so that it writes a cookie for when the inline element 'contributors' is toggled and remembers the setting on the next page, or when the user returns to the site (hopefully).

    Thanks in Advance,
    Nicht Mich

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,566 Times in 2,544 Posts
    This is easily done with a cookie. Here's an example:-


    <SCRIPT language="Javascript">
    function setCookieValue(name, value)
    var name="myname";
    var value="set";
    var expiry = new Date();
    expiry.setTime(expiry.getTime() + (100*60000));
    document.cookie = name + "=" + value + ";expires=" + expiry.toGMTString() +";";

    function getCookieValue(name)
    var temp = document.cookie + ";";
    var Pos = temp.indexOf("=", temp.indexOf(name + "="));
    if (temp.indexOf(name + "=") == -1) return "";
    ret1=temp.substring(Pos + 1, temp.indexOf(";", Pos));
    if (ret1 == "set") {

    return ret1;



    Then on each page you need:-

    <body onLoad="getCookieValue(name)">

    and some code to do whatever you want to happen if the cookie is set.

    Hope this moves you forward!


    Posting Permissions

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