/    Sign up×
Community /Pin to ProfileBookmark

cookies in show/hide divs

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]
<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>
[/code]

thanks…

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinOct 21.2009 — use [B]onbeforeunload="doStuff()"[/B] in your body tag
Copy linkTweet thisAlerts:
@silvameloauthorOct 21.2009 — Thank you, justinbarneskin, but it doesn't work... Do you know another way to make the thing happen?
Copy linkTweet thisAlerts:
@silvameloauthorOct 23.2009 — I need it persists after I close and open the navigator again...

that's what I do:

[code=html]
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
}
[/code]


and it occasiones no effect...

do you know what more I can try?
Copy linkTweet thisAlerts:
@justinbarneskinOct 23.2009 — SetCookie (name, value, per, exp)

if you don't have the argument [I]per[/I] when calling your function

[I]exp[/I] will move up to be interpreted as [I]per[/I]
×

Success!

Help @silvamelo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...