Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    1
    Thanks
    0
    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:

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

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

    Code:
    <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
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    This is easily done with a cookie. Here's an example:-

    <html>
    <head>

    <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") {
    setCookieValue();
    }

    return ret1;
    }

    </SCRIPT>

    </head>

    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
    •