Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Switch Content - open one on page load?

  1. #1
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch Content - open one on page load?

    http://www.dynamicdrive.com/dynamici...tchcontent.htm

    I'm using the switch content expand/contract script and I'm trying to get it so that one of the links is already open when the page loads (so that clicking link closes it). I want all of my other ones closed, but just the one open. I've tried a few things, but not having much success. thanks.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    HTML Code:
    <body onload="expandcontent(document.getElementById('idOfLinkToOpen'), 'contentid');">
    Note that you must give the link to open an ID.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    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

    I don't think that will work. Try this - first choose the content you want expanded onload, let's say 'sc2'. Then find this line in the script at the end of the do_onload function:
    Code:
    revivestatus()
    just after it add these two lines:
    Code:
    sweeptoggle('contract')
    expandcontent(statecollect[1].parentNode, 'sc2');
    Notice where sc2 (the content we want expanded) is and that the number in red (1) is one less than (2), the number of the sc# content we want expanded.
    - John
    ________________________

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

  4. #4
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, John, that worked great. Now that I got this figured out, I'm wondering if there's a way that I can get it so that, say I open sc1, (which, because of persist value, causes the sc2 to close) then close sc1 and sc2 will open back up. Essentially, I want it so they are never all closed, but always defaults back to sc2 open. (The reason for this is because I have the expanding div's in a table on another part of the page, so it's remote controlled content and I never want it to be blank--I want it to go back to sc2 when I close the others. Hope I explained clearly enough. Thanks again for the help.

  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

    That can be done but, it will also logically give rise to this situation:

    If sc2 (or whatever content you have used for this [see *]) is the only one open and the user tries to contract it, nothing will happen.

    If that is OK with you, here's how - Go back to the two lines we added before at the end of do_onload() and remove this one:
    Code:
    expandcontent(statecollect[1].parentNode, 'sc2');
    At the end of the sweeptoggle(ec) function, just after:
    Code:
    revivestatus()
    add these two lines:*
    Code:
    if (ec=="contract")
    expandcontent(statecollect[1].parentNode, 'sc2');
    Finally, at the end of the expandcontent(curobj, cid) function where it looks like this:
    Code:
    revivestatus()
    }
    }
    }
    Make it look like this:
    Code:
    revivestatus()
    }
    }
    for (i = 0; i < ccollect.length; i++)
    if (ccollect[i].style.display=='block')
    return;
    sweeptoggle('contract')
    }
    * If you want a different one than sc2 to be given this treatment,
    change the sc2 to the id of your choice and make
    the number 1 in statecollect[1] be one less than the sc#.
    - John
    ________________________

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

  6. #6
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for all your help. About the not being able to contract it, I'm ok with that. Incidentally, because of the way I'm setting it up, I don't even have a link to expand/contract it, so it's fine for me. I'm really impressed with your ability. Thanks so much.

  7. #7
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    A new question about this script: How would I add a link to contract the content at the end of the expanded content (so that the variable "contractsymbol" is displayed at the bottom of the expanded div as well.) With lengthy content, people might get lost, so it would be helpful to have an extra 'close' link. I thought it would be easier to figure out, but I didn't have much luck on my own. (It would be nice for the script to do it automatically, but if I have to manually input it after each div, that's ok too.) Thanks.

  8. #8
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Actually, I just now realized an obvious way to do that manually
    Code:
    <a href="#" onClick="expandcontent(this, 'sc2')" style="font-size:12px; 
    color:003366; cursor:hand; cursor:pointer">Close</a>
    so I guess I don't need help with this, unless you have a suggestion for generating this automatically in the javascript.

  9. #9
    Join Date
    Jul 2005
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    On second thought, maybe it would be better not to automatically generate a contract link...it would most likely interfere with the one that automatically opens on pageload (from above), since it can't be closed, and that would just confuse visitors.

  10. #10
    Join Date
    Aug 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unable to work, error:

    I am using the same script and I also want to set one ID sc4 to be open onload. However, I cannot get it to work using the method posted here.

    I keep getting the following error message on the window.

    ( statecollect.2.parentNode is null or not an object )

    I did change the code to below so I can freely contract expand, content
    ====================================
    <div id="sc4" class="switchcontent">

    :::::MY CONTENT::::

    </div>
    ====================================

    Note: For own stupid reason, I have class sc1, sc2, and sc4, sc5,....etc

    I skipped sc3 on purpose so I can use it as a add on content later

    Please advise so I can get this wonderful script to work

    Huang

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
  •