Results 1 to 6 of 6

Thread: Switch Content Script cookie

  1. #1
    Join Date
    Nov 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch Content Script cookie

    1) Script Title: Switch Content Script

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...tchcontent.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:
    Code:
    /***********************************************
    * 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

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    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?

  3. #3
    Join Date
    Nov 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yeah, that would be a good solution How is this possible?

  4. #4
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    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.

  5. #5
    Join Date
    Nov 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Has it been done yet?

    Thanks
    Oli

  6. #6
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •