PDA

View Full Version : persistent cookies



playernovis
07-29-2007, 08:41 PM
I love this script http://www.dynamicdrive.com/dynamicindex1/navigate2.htm

the only problem is that I would like it to remember the setting (open/close) even when user closes IE (persisten cookies instead of session cookies).

Is there an easy way to modify this script? Please note I'm really rookie.

thanks a lot
jiri

jscheuer1
07-30-2007, 04:37 AM
Replace these two functions:


function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="block")
selectedItem+=ccollect[inc].id+"|"
inc++
}

document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload(){
uniqueidn=window.location.pathname+"firsttimeload"
getElementbyClass("switchcontent")
if (enablepersist=="on" && typeof ccollect!="undefined"){
document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0"
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
revivecontent()
}
}

With these:


function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="block")
selectedItem+=ccollect[inc].id+"|"
inc++
}
var date = new Date();
date.setTime(date.getTime()+(10*86400000));
var expires = "; expires="+date.toGMTString();
document.cookie=window.location.pathname+"="+selectedItem+expires;
}

function do_onload(){
uniqueidn=window.location.pathname+"firsttimeload"
getElementbyClass("switchcontent")
if (enablepersist=="on" && typeof ccollect!="undefined"){
var date = new Date();
date.setTime(date.getTime()+(10*86400000));
var expires = "; expires="+date.toGMTString();
document.cookie=(get_cookie(uniqueidn)==""? uniqueidn+"=1" : uniqueidn+"=0")+expires;
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
revivecontent()
}

Notice the two red 10's in the above new functions. These set the two cookies (the script already required two cookies) for 10 days. Increase or decrease as desired. Both values should be set to the same number though.

playernovis
07-30-2007, 03:17 PM
thanks a lot John !! This is exactly what I need.

I also changed the calls little bit so the cookie is not set only when you leave the page, but onClick as well.

thanks again
jiri