...

View Full Version : Collapsible tables with persistent cookie?



Jerry Curl
01-02-2008, 10:13 PM
Hi all,

I've got a problem with some collapsible tables I want to include on a phpBB3 template. Basically, I want the forum categories on the main page to be collapsible like vBulletin, and have the user's browser remember the state of the tables. Not being proficient in JavaScript at all, I'm having a terrible time getting it to work. My own attempts at trying to fix this just bust the code and the DHTML no longer works.

The script I'm using now would work great except the cookie shows it expires upon quiting the browser. I'd like it to expire in 90 days or a year. Something like that.

Here is my test site (http://realdealstubblefield.com/phpbb3_demo/index.php?style=7) so you can see what I mean. And here is the code I have in the head content.


<script language="javascript">
function init()
{
var cookie = getCookie('collapse_obj');
if(cookie)
{
var values = cookie.split(',');

for(var i = 0; i < values.length; i++)
{
var itm = getItem(values[i]);

if(itm)
itm.style.display = 'none';
}
}
}

function makeCookie(name, value, time()+604800);
{
var cookie = name + '=' + escape(value) + ';';
document.cookie = cookie;
}

function getCookie(name)
{
if(document.cookie == '')
return false;

var firstPos;
var lastPos;
var cookie = document.cookie;

firstPos = cookie.indexOf(name);

if(firstPos != -1)
{
firstPos += name.length + 1;
lastPos = cookie.indexOf(';', firstPos);

if(lastPos == -1)
lastPos = cookie.length;

return unescape(cookie.substring(firstPos, lastPos));
}

else
return false;
}

function getItem(id)
{
var itm = false;
if(document.getElementById)
itm = document.getElementById(id);
else if(document.all)
itm = document.all[id];
else if(document.layers)
itm = document.layers[id];

return itm;
}

function toggleItem(id)
{
itm = getItem(id);

if(!itm)
return false;

if(itm.style.display == 'none')
itm.style.display = '';
else
itm.style.display = 'none';

////////////////////

cookie = getCookie('collapse_obj');
values = new Array();
newval = new Array();
add = 1;

if(cookie)
{
values = cookie.split(',');

for(var i = 0; i < values.length; i++)
{
if(values[i] == id)
add = 0;
else
newval[newval.length] = values[i];
}
}

if(add)
newval[newval.length] = id;

makeCookie('collapse_obj', newval.join(','));

return false;
}
</script>


Thanx. :)

Jerry Curl
01-03-2008, 01:45 AM
No one can help I take it?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum