Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cookies in show/hide divs

Status
Not open for further replies.

silvamelo

Programmer
Oct 20, 2009
5
BR
This script works fine, but it saves cookies only when the navigator is open. Closing it, the cookies stop to save. How to resolve this?

Code:
<html>
<head>
<script type="text/javascript">
function getCookieVal(offset) {
  endstr = document.cookie.indexOf (";", offset)
  if(endstr == -1) endstr = document.cookie.length
  return unescape(document.cookie.substring(offset, endstr))
}

function GetCookie(name) {
  arg = name + "="
  alen = arg.length
  clen = document.cookie.length
  var i = 0
  while (i < clen) {
    j = i + alen
    if(document.cookie.substring(i, j) == arg) return getCookieVal(j)
    i = document.cookie.indexOf(" ", i) + 1
    if(i == 0) break
  }
  return null
}

function SetCookie (name, value, per, exp) {
  cstr = name + "=" + escape(value) + ";"
  if(per){
    addtime=(exp>0) ? exp : 31536000000
    expdate = new Date()
    expdate.setTime(expdate.getTime() + addtime)
    expdate = expdate.toGMTString()
   cstr+=" expires=" + expdate
  }
  document.cookie = cstr
}

function DeleteCookie(name) {
  exp = new Date()
  exp.setTime (exp.getTime() - 1)
  cval = GetCookie(name)
  if(cval != null)
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString()
}



function setDisplay(div,disp)
{
  if (disp!=null)
   div.style.display=disp;
}

function expandCollapse(id)
{
  var data=id.split(':'); 
  var div = document.getElementById(data[0]);
  if (div.style.display=='none')
  {
    div.style.display='block';
    SetCookie(id,'block');
  }
  else
  {
    div.style.display='none';
    SetCookie(id,'none');
  }
  
  return false;
}

function setState(pageID)
{
  var div;
  var i=1;
  while (div=document.getElementById('Div'+(i++))) 
   setDisplay(div,GetCookie(div.id+':'+pageID));
}
</script>
</head>
<body onload="setState('page1')">
<p>
<a href="#" onclick="return expandCollapse('Div1:page1')">Expand/Collapse</a>
<div id ="Div1" style="display:block;">Div1 content</div>
</p>
<p>
<a href="#" onclick="return expandCollapse('Div2:page1')">Expand/Collapse</a>
<div id ="Div2" style="display:block;">Div2 content</div>
</p>
<p>
<a href="#" onclick="return expandCollapse('Div3:page1');">Expand/Collapse</a>
<div id ="Div3" style="display:block;">Div3 content</div>
</p>
</body>
</html>
thanks...
 
I'm guessing you didn't write the SetCookie function, otherwise you would have understood the parameters it takes...

Try passing things for the "per" (Persist, I assume) and "exp" (Expires, I assume) parameters.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
You're right... I'm a newbe! But I know there's something wrong with the "addtime" parameter... I've tried to change it at same ways like (31*24*60*60), and I got nothing! can you show me what I have to do, Dan? Thank you.
 
I need it persists after I close and open the navigator again...

Ok, that's what I do:

Code:
function SetCookie (name, value, per, exp) {
  cstr = name + "=" + escape(value) + ";"
  if(per){
    addtime=(exp*24*60*60*1000)
    expdate = new Date()
    expdate.setTime(expdate.getTime() + addtime)
    expdate = expdate.toGMTString()
   cstr+=" expires=" + expdate
  }

and it occasiones no effect...
do you know what more I can try?
 
Help us out here, because we have no idea what you are passing in to that function!

So, tell us, what does cstr show right before it's loaded into document.cookie?



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Well, other guy wrote that... so I can't explain... but the only function I need for this script is show/hide with cookies, to make possible visitors ocult some informations of the site and remember them to the next visit... Can it be possible with this script?
 
Well, other guy wrote that... so I can't explain

So you have no access to modify the code to put an "alert" in showing the value of "cstr" before it sets it into the cookie?




Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
OK - so if you've got access to modify the script, then modify it to alert cstr before it's assigned to document.cookie, and tell us what it say.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top