PDA

View Full Version : Cookies



Tabo
04-01-2007, 09:49 AM
I have this:



<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.)

jscheuer1
04-01-2007, 11:25 AM
<!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>

Tabo
04-01-2007, 01:03 PM
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.

jscheuer1
04-01-2007, 03:23 PM
Most likely it is an onload conflict. It could be other things.