Results 1 to 7 of 7

Thread: Switch Content Script - persist problem

  1. #1
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch Content Script - persist problem

    1) Script Title: Switch Content Script

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...tchcontent.htm

    3) Describe problem: This is a great script but I would like have the persistance feature enabled (so that it remembers the header states) AND if it finds no cookie (default state when starting a session) it is possible to specify which content to expand by default.
    By default when
    xxxxxx.setPersist(true)
    all of my content blocks are contracted.

    Is it possible to modify the following block of code:
    Code:
    switchcontent.getCookie=function(Name){ 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
    }
    so that it if it DOESN'T find a cookie then it expands specific content blocks?

    All help and support greatly appreciated.
    Thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can feed it the numbers of the contents you want open:

    Code:
    switchcontent.getCookie=function(Name){ 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return "0,1"
    }
    Since things are usually numbered in javascript from 0 to whatever, the above (red) would represent the first two content blocks.

    If you have only one instance of the script on a page, that is all you would need.

    If you have more than one, you have to test for its class name:

    Code:
    switchcontent.getCookie=function(Name){ 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    if(Name=='switchgroup2')
    return "0,1"
    return ""
    }
    This would select switchgroup2 to open its first two blocks but, only if there were no cookie data for switchgroup2. You can use as many of these as you need, each with its own default block state settings:

    Code:
    switchcontent.getCookie=function(Name){ 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    if(Name=='switchgroup1')
    return "1,2"
    if(Name=='switchgroup2')
    return "0,1"
    return ""
    }
    This will only work with persistence true and these groups probably also would need collapse previous false if they were set to have more than one block open using the above method. If persist is false, you can use the default expanded property (as documented on the demo page):

    Code:
    <script type="text/javascript">
    
    var joeexample=new switchcontent("switchgroup2", "p") //Limit scanning of switch contents to just "p" elements
    joeexample.setStatus('[open] ', '[closed] ')
    joeexample.setColor('green', 'red')
    joeexample.collapsePrevious(false) //Allow more than 1 content to be open simultanously
    joeexample.setPersist(false)
    joeexample.defaultExpanded(0,1)
    joeexample.init()
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks John

    That did the trick!

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

    Default

    Maybe I'll missing something in your question here, but why not just use the defaultExpanded() feature that's built in? For example:

    Code:
    joeexample.defaultExpanded(0, 1)
    Would cause the first and 2nd contents to be expanded by default if no cookie is found (ie: Persist is enabled but this is the 1st page load).

    Edit: Ah nevermind, I didn't realize the script behaved as such that if cookie is enabled, the defaultExpanded() function is not run altogether. Looks like a design flaw that I should probably fix...
    Last edited by ddadmin; 01-20-2007 at 10:20 AM.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by ddadmin View Post
    Edit: Ah nevermind, I didn't realize the script behaved as such that if cookie is enabled, the defaultExpanded() function is not run altogether. Looks like a design flaw that I should probably fix...
    How about:

    Code:
    function switchcontent(className){
    if(!switchcontent.ar)
    switchcontent.ar=[]; //array to cache instances of switchcontent on page
    switchcontent.ar[switchcontent.ar.length]=this;
    this.className=className
    this.collapsePrev=false //Default: Collapse previous content each time
    this.enablePersist=false //Default: Disable session only persistence
    //Limit type of element to scan for on page for switch contents if 2nd function parameter is defined, for efficiency sake (ie: "div")
    this.filter_content_tag=(arguments.length==2)? arguments[1].toLowerCase() : ""
    }
    and:

    Code:
    switchcontent.getCookie=function(Name){ 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    for (var i_tem = 0; i_tem < switchcontent.ar.length; i_tem++)
    if(Name=switchcontent.ar[i_tem].className&&switchcontent.ar[i_tem].expandedindices)
    return switchcontent.ar[i_tem].expandedindices;
    return ""
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    Hmm there's actually no need to tamper with the cookies function. Basically where I went wrong with the original code was to completely ignore the defaultExpanded() function if persistence is set, which is too presumptuous. It should only be ignored if persistence is set AND the cookie actually contains a value, indicating this isn't the first time the page is loaded and to use that value.

    So the only line in the script that needs to be changed is:

    Code:
    var opencontents_ids=(this.enablePersist && switchcontent.getCookie(this.className)!="")? ','+switchcontent.getCookie(this.className)+',' : (this.expandedindices)? ','+this.expandedindices+',' : ""
    I'll probably do some more testing before updating the script with the above fix. I know, looking at someone else's code can be like looking for a needle in the haystack .

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Makes sense if it works (looks to). I was already focused on the particular part of the code that I chose to modify from the way the original question in this thread was posed.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •