Forum Moderators: open

Message Too Old, No Replies

Looking for Cookie Based Hide Show Div Tag

         

RegGFX

8:31 pm on Jul 11, 2006 (gmt 0)

10+ Year Member



I'm basically looking for script that uses hide show functionallity. With the addition of save state ability based on a cookie that remembers what item the user last expanded for his/her previous visit.

In other words lets say a user expands a link that has a list of information.

Then the user leaves the page and comes back to the same page. It would be ideal if that page remembers(via cookie) what the user last expanded.

Does ANYONE... know where or can perhaps paste some sample code?

Anyone... Please RSVP

Thanks

RonPK

7:22 am on Jul 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That shouldn't be too complicated. What stops you from giving it a try yourself?

birdbrain

12:11 pm on Jul 12, 2006 (gmt 0)



Hi there RegGFX,

have a look at this example, it may suit your requirements..


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>two state cookie</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
<!--
#setup {
color:#000;
background-color:#fff;
}
#info {
width:400px;
padding:30px 20px;
border:3px double #000;
margin:20px auto;
}
#info p {
margin:0;
text-align:justify;
}
.hide {
display:none;
}
.show {
display:block;
}
-->
</style>

<script type="text/javascript">
<!--
var state;
window.onload=function() {
obj=document.getElementById('info');
state=(state==null)?'hide':state;
obj.className=state;

document.getElementById('setup').onclick=function() {
obj.className=(obj.className=='show')?'hide':'show';
state=obj.className;
setCookie();
return false;
}
}

function setCookie() {
exp=new Date();
plusMonth=exp.getTime()+(31*24*60*60*1000);
exp.setTime(plusMonth);
document.cookie='State='+state+';expires='+exp.toGMTString();
}

function readCookie() {
if(document.cookie) {
state=document.cookie.split('State=')[1];
}
}
readCookie();
//-->
</script>

</head>
<body>

<div>
<a id="setup" href="#">show/hide information</a>
</div>

<div id="info">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin massa. Nam vehicula.
Morbi velit nisi, mollis id, ultrices luctus, adipiscing sit amet, lectus. Nunc rhoncus
nisl ac enim. Maecenas vestibulum dolor ut velit. Maecenas condimentum pulvinar purus.
Pellentesque ac ipsum. Curabitur sodales, elit vel molestie hendrerit, elit odio rhoncus tellus,
nec gravida enim urna id velit. Donec nec tellus. Vestibulum nulla. Curabitur enim arcu,
ornare id, placerat eget, nonummy vitae, mauris. Nulla rutrum semper odio. Duis vulputate
ornare mauris. Praesent eget nibh sed ante ultricies scelerisque. Duis eget felis ut arcu porta
bibendum. Mauris rutrum. Vivamus consectetuer purus sit amet mi. Suspendisse eu augue.
</p>
</div>

</body>
</html>

birdbrain