View Full Version : Site wide cookie setting and retrieval

01-15-2003, 07:08 PM
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

Philip M
01-15-2003, 08:04 PM
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!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum