View Full Version : Switch Content - open one on page load?

07-22-2005, 04:28 AM

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.

07-22-2005, 06:49 AM
<body onload="expandcontent(document.getElementById('idOfLinkToOpen'), 'contentid');">

Note that you must give the link to open an ID.

07-22-2005, 09:04 AM
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:

revivestatus()just after it add these two lines:

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.

07-23-2005, 12:19 AM
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.

07-23-2005, 08:08 AM
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:

expandcontent(statecollect[1].parentNode, 'sc2');At the end of the sweeptoggle(ec) function, just after:

revivestatus()add these two lines:*

if (ec=="contract")
expandcontent(statecollect[1].parentNode, 'sc2');Finally, at the end of the expandcontent(curobj, cid) function where it looks like this:

}Make it look like this:

for (i = 0; i < ccollect.length; i++)
if (ccollect[i].style.display=='block')
}* 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#.

07-25-2005, 08:03 PM
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.

08-02-2005, 02:07 AM
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.

08-02-2005, 02:17 AM
Actually, I just now realized an obvious way to do that manually
<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.

08-02-2005, 02:21 AM
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.

08-09-2005, 06:03 PM
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::::


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


08-10-2005, 02:29 PM

Probably has to do with what you mentioned. Since your DOM (roughly speaking, the order of elements on the page, in this case) is different than the script expects, it is failing. Either make a dummy of the missing content with no text in it or bite the bullet and follow the order laid out in the script. Also, are you using the + - symbols or at least something in their place? If not, this could be throwing things off as well.