fatrat
01-10-2007, 11:19 PM
hi, id like to set up my website so that lots of the contents is collapsible and the state is remembered using cookies.
here is my testing code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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 changeme(id)
{
state = document.getElementById(id).style.display;
if (state == 'none')
{
state='block';
document.getElementById(id).style.display = 'block';
} else
{
state='none';
document.getElementById(id).style.display = 'none';
}
createCookie(id, state, 30);
}
function checkcookies()
{
var x = readCookie('me')
if (x == 'none')
{
document.getElementById('me').style.display = 'none';
}
}
</script>
</head>
<body onload="checkcookies()">
<div class="standardheader">
<h1> <span class="standardheader-mid"><span class="standardheader-left"></span> <span class="standardheader-right"><a href="#" onclick="changeme('me')">+</a></span> Navigation</span></h1>
<ul class="row1list" id="me">
<li><a href="{U_CREATEACLAN}">Create a Clan</a></li>
<li><a href="{U_ADDSCRIM}">Add a Scrim</a></li>
</ul>
</div>
<div class="standardheader">
<h1> <span class="standardheader-mid"><span class="standardheader-left"></span> <span class="standardheader-right"><a href="#" onclick="changeme('you')">+</a></span> Navigation</span></h1>
<ul class="row1list" id="you">
<li><a href="{U_CREATEACLAN}">Create a Clan</a></li>
<li><a href="{U_ADDSCRIM}">Add a Scrim</a></li>
</ul>
</div>
hi
</body>
</html>
atm,
var x = readCookie('me')
onload checks for the state of the 'me' element and sets it.
i need it so the onload it sets the state of all the elements.
is it possible for it the 'readCookie' function to read through all cookies, if the value of the cookie is none or block, then it will set the element display:xxx.
i dont know if that the best way of getting the state of the possible elements on a page.
every page will contain different elements that will be collapsible. so, it needs to be an efficient way of getting the cookies that match the elements for that page. but, the each page is also dynamic, an element that appears one time might not appear the next time.
i guess i could try to manipulate my php to to figure out what elements are being shown before i call the header.php, then put the element list in the onload function and call the cookies that way.
or i could use php to read the cookies and set the states before the page is sent to the the browser.
i am completely new to cookies, so i have no clue what the best way is.
thanks for any help.
here is my testing code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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 changeme(id)
{
state = document.getElementById(id).style.display;
if (state == 'none')
{
state='block';
document.getElementById(id).style.display = 'block';
} else
{
state='none';
document.getElementById(id).style.display = 'none';
}
createCookie(id, state, 30);
}
function checkcookies()
{
var x = readCookie('me')
if (x == 'none')
{
document.getElementById('me').style.display = 'none';
}
}
</script>
</head>
<body onload="checkcookies()">
<div class="standardheader">
<h1> <span class="standardheader-mid"><span class="standardheader-left"></span> <span class="standardheader-right"><a href="#" onclick="changeme('me')">+</a></span> Navigation</span></h1>
<ul class="row1list" id="me">
<li><a href="{U_CREATEACLAN}">Create a Clan</a></li>
<li><a href="{U_ADDSCRIM}">Add a Scrim</a></li>
</ul>
</div>
<div class="standardheader">
<h1> <span class="standardheader-mid"><span class="standardheader-left"></span> <span class="standardheader-right"><a href="#" onclick="changeme('you')">+</a></span> Navigation</span></h1>
<ul class="row1list" id="you">
<li><a href="{U_CREATEACLAN}">Create a Clan</a></li>
<li><a href="{U_ADDSCRIM}">Add a Scrim</a></li>
</ul>
</div>
hi
</body>
</html>
atm,
var x = readCookie('me')
onload checks for the state of the 'me' element and sets it.
i need it so the onload it sets the state of all the elements.
is it possible for it the 'readCookie' function to read through all cookies, if the value of the cookie is none or block, then it will set the element display:xxx.
i dont know if that the best way of getting the state of the possible elements on a page.
every page will contain different elements that will be collapsible. so, it needs to be an efficient way of getting the cookies that match the elements for that page. but, the each page is also dynamic, an element that appears one time might not appear the next time.
i guess i could try to manipulate my php to to figure out what elements are being shown before i call the header.php, then put the element list in the onload function and call the cookies that way.
or i could use php to read the cookies and set the states before the page is sent to the the browser.
i am completely new to cookies, so i have no clue what the best way is.
thanks for any help.