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:
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.
so i would have to use php to output this list?
if im going to use php to output the list of elements on the page, i might as well use it to read the cookie and set the states hadnt i?
like i said, every page will have different elements on it.
function checkcookies()
{
var x = readCookie('me')
document.getElementById('me').style.display = x;
var y = readCookie('you')
document.getElementById('you').style.display = y;
}
first of all i dont know what you do in php but you never start with 1 when your talking arrays atleast not in javascript,c++,c and other important programming languages you dont. you start with 0 such as
in php its default to start at 0, but i think u can start at what u like. at least i think so...
yay, that fixed it, ty.
man, i would be here all night and not figuring that out. I was even look at an example of building an array and had kinda missed that bit.
i read this:
Quote:
quote[0]
I know, zero instead of 1. Arrays just work that way, the first element is always zero and the last element is always one less than what you define as the number of elements.
and promptly forgot it, didnt even realise i hadnt started it 0.
you could by default have all the divs set to hidden
Code:
<div style="display:none">
</div>
since your javascript is going to display the one that should be displayed first anyways
thats not what you want you want the ul's to hide let me do some more poking
Yep it works with ul's so go like this
Code:
<ul id="ids" style="none">
<li>adfaD</li>
</ul>
and if they have never visited the site they will all be hidden
but if they have been to the site before and still have the cookies
then the ones they want hidden should be hidden by default then when
the cookies load the ones they want to view should show up
i guess i could set to to state='none' or state=''
i assume this is caused because the default setting is in the external css file, but then i set it again using the cookie info. nvm, coz the state='' works
edit: nope, the state='' caused more probs i think.