View Full Version : Switch Content I Script :: Set open section onload ...

06-02-2006, 09:02 PM
I'm using this script on our company intranet on a few pages. I'm trying to accomplish this:

User clicks on link to section2 (or 3 or 4 or whatever). Page with SC script loads with section2 (or 3 or 4 or whatever) expanded. I added a small bit of PHP code to change the display status of the specific sections to 'block'.
So the divs look like this --

<div id="gen" class="switchcontent" style="display:<? echo $genblock; ?>;">
and the link to the page is like so

<a class="lev2" href="telephone/?svcblock=block#svc-contact">Service Specific</a>
(note, it does not matter if I specify index.htm in the link or not)

This works perfect as long as I do not have enablepersist ON. As soon as enable persist is on, the whole concept goes out the window.

My apologies if this has been asked before, but can this script be used with enablepersist on AND allow the user to click links which open the page with a specific section expanded?


06-02-2006, 09:29 PM
With the persist enabled, what do you want to have happen? Do you want all previously opened content to remain open plus the php linked item or do you want the previously opened contents to close except when no php linked content is passed?

In either case, it would involve a query* of all the elements before the persistence code was executed to determine if any already had style.display='block' via php. And then either adding this to the list of open items found in the cookie or aborting the cookie restoration if an open php link div was found, depending upon how you wanted it to behave.

*In most of these type scripts there is already code that does the above mentioned query in the onunload phase of cookie creation. This could either be copied or reused.

Please provide a link to the dd script in question and to your page.

06-02-2006, 09:50 PM
Unfortunately I cannot provide a link to the page -- it's on our company intranet. I can tell you the script is being used as is -- I've made no mods/changes to it other than turning persist or collapse previous on/off.

Below is an example of the code in question:

<a name="general"></a>
<dt onClick="expandcontent(this, 'gen')"><a href="#general" class="teltitletest">Systems, Security &amp; Emergency</a>

<dd><div id="gen" class="switchcontent" style="display:<? echo $genblock; ?>;">
<ul id="tdtop">
<li><a href="/link/to/file.htm">Enterprise Network</a>
<li><a href="/link/to/file2.htm">IP Network</a>

With persist on, it doesn't matter if other sections stay opened/closed when the links are clicked -- the only thing that matters is that the section they requested is opened.

I've also noticed another problem with these pages ... they work in IE, but not Firefox or NN8. I'll have to investigate that.


06-02-2006, 10:24 PM
Well, I cannot comment on problems with other browsers without a link to your page, the script as presented on DD works in IE and FF, other than to say your use of lists instead of the provided heading/division pairs might be the culprit. You could try using a heading and then have a list inside the division.

Anyways, I also wanted a link to the script here on DD to save me time hunting for it and to be sure I had the right one. I think I found it and have come up with this code that should work, substitute (changes red):

function do_onload(){
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
if (enablepersist=="on" && ccollect.length>0){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="block")
if (selectedItem==""){
document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0"
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
if (ccollect.length>0 && statecollect.length>0)

For the existing function of the same name, don't try to edit it, just replace it - to make sure that you don't miss anything and that I didn't miss highlighting anything. It will detect if php has set any of the items to display:block and skip the whole cookie business if it has, otherwise, persistence will work as usual.