PDA

View Full Version : Switch Content Script cookie



o.ash92
11-19-2006, 06:33 PM
1) Script Title: Switch Content Script

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/switchcontent.htm (but using edited version, find code below)

3) Describe problem: I am using this script on my member system. When you are logged in, you can see around 5 boxes with this script fitted onto them. Meanwhile, when you're logged out, you can see about 3.
Because of the way the script has been put together, it appears that one cookie is used to remember the data for all 5 boxes, and when you logout, the 3 boxes don't recognise this cookie but there are 2 other boxes' data inside.

In other words, when you're logged in the five boxes create one cookie so that it will remember which box you chose to expand/collapse previously. But when you log out, the three boxes can't read from this cookie because there are two other boxes which it doesn't recognize.

Here is the modified version of the script which I am using:

/***********************************************
* Switch Content script- Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use. Last updated April 2nd, 2005.
* Visit http://www.dynamicdrive.com/ for full source code
* Modified to use site wide cookies by jscheuer1 in
* http://www.dynamicdrive.com/forums
***********************************************/

var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)
var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)

var contractsymbol='[-]' //HTML for contract symbol. For image, use: <img src="whatever.gif">
var expandsymbol='[+]' //HTML for expand symbol.

////////////////Stop Editing////////////////

function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name)
{
createCookie(name,"",-1);
}

if (document.getElementById){
document.write('<style type="text/css">')
document.write('.switchcontent{display:none;}')
document.write('</style>')
}

function getElementbyClass(rootobj, classname){
var temparray=new Array()
var inc=0
var rootlength=rootobj.length
for (i=0; i<rootlength; i++){
if (rootobj[i].className==classname)
temparray[inc++]=rootobj[i]
}
return temparray
}

function sweeptoggle(ec){
var thestate=(ec=="expand")? "block" : "none"
var inc=0
while (ccollect[inc]){
ccollect[inc].style.display=thestate
inc++
}
revivestatus()
}


function contractcontent(omit){
var inc=0
while (ccollect[inc]){
if (ccollect[inc].id!=omit)
ccollect[inc].style.display="none"
inc++
}
}

function expandcontent(curobj, cid){
var spantags=curobj.getElementsByTagName("SPAN")
var showstateobj=getElementbyClass(spantags, "showstate")
if (ccollect.length>0){
if (collapseprevious=="yes")
contractcontent(cid)
document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
if (showstateobj.length>0){ //if "showstate" span exists in header
if (collapseprevious=="no")
showstateobj[0].innerHTML=(document.getElementById(cid).style.display=="block")? contractsymbol : expandsymbol
else
revivestatus()
}
}
}

function revivecontent(){
contractcontent("omitnothing")
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
for (i=0; i<selectedComponents.length-1; i++)
document.getElementById(selectedComponents[i]).style.display="block"
}

function revivestatus(){
var inc=0
while (statecollect[inc]){
if (ccollect[inc].style.display=="block")
statecollect[inc].innerHTML=contractsymbol
else
statecollect[inc].innerHTML=expandsymbol
inc++
}
}



function getselectedItem(){
if (readCookie('switch_content') != null){
selectedItem=readCookie('switch_content')
return selectedItem
}
else
return ""
}

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

createCookie('switch_content', selectedItem)
}

function do_onload(){
uniqueidn="firsttimeload"
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
if (enablepersist=="on" && ccollect.length>0){
if (readCookie(uniqueidn)==null)
createCookie(uniqueidn, 1)
else
createCookie(uniqueidn, 0)
firsttimeload=(readCookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
revivecontent()
}
else
sweeptoggle('expand')
if (ccollect.length>0 && statecollect.length>0)
revivestatus()
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate

How can I solve this problem?

Many Thanks,
Oli

ddadmin
11-22-2006, 10:48 AM
Just so I understand correctly, you have five different switch contents running on the same page right, and you wish each of them to have their own cookie setting?

o.ash92
11-22-2006, 04:17 PM
Yeah, that would be a good solution :) How is this possible?

ddadmin
11-23-2006, 01:34 AM
Ok, looks like I need to update this script for allowing multiple switch contents per page. There's no "quick" fix for the issue. I'm not sure when I can get to the update, though atmost within the next 2 weeks.

o.ash92
12-09-2006, 04:08 PM
Has it been done yet?

Thanks
Oli

ddadmin
12-10-2006, 07:02 AM
Unfortunately not yet. I've been preoccupied with a whole bunch of other stuff lately. It will be done for sure, though to be safe I'd safe within 2 weeks from now. :)