This script works with virtually any content on your page to make them expandable. The process is always the same, which consist of the below 2 steps:
Step 1: Define your contents and content headers to participate in the "switching". For example:
<h3 id="bobcontent1-title" class="handcursor">What is JavaScript?</h3>
<div id="bobcontent1" class="switchgroup1">
JavaScript is a scripting language originally developed by Netscape to add interactivity
and power to web documents. It is purely client side, and runs completely on the client's browser and computer.
</div>
<h3 id="bobcontent2-title" class="handcursor">Difference between Java & JavaScript?</h3>
<div id="bobcontent2" class="switchgroup1">
Java is completely different from JavaScript.
The former is a compiled language while the later is a scripting language.
</div>
Here I have two contents (plus their headers) I wish to contract/ expand dynamically via this script. The only protocol you must follow is to give each content container a unique ID (ie: "bobcontent1") and a common CSS class name to group them together (ie: "switchgroup1"). Then for each content's corresponding header, assign it the same ID as the content itself, but suffixed with "-title" (ie: "bobcontent1-title"). This conversion lets the script know what belongs to what.
Warning: The tags used to act as the content headers (ie: "bobcontent1-title") must be a container tag itself, whether H1, H2, P, DIV, SPAN etc. If you wish to use images as the content headers, wrap it inside a container tag, for example:
<span id="bobcontent1-title" class="handcursor"><img src="header.gif" /></span>
Step 2: Once your switch contents are defined, you're ready to make them expandable/ contractible, by declaring the JavaScript invocation code following it:
<script type="text/javascript">
var bobexample=new switchcontent("switchgroup1", "div") //Limit scanning of switch contents to just "div" elements
bobexample.setStatus('<img src="open.png" /> ', '<img src="close.png" /> ')
bobexample.setColor('darkred', 'black')
bobexample.setPersist(true)
bobexample.collapsePrevious(true) //Only one content open at any given time
bobexample.init()
</script>
And that's the gist of it, but wait...
You can specify an external file as the content of any header within a Switch Content group, so that the content is fetched on demand only when the header is actually expanded (either automatically or by user action). To do this, you'll probably want to first empty out any inline content that exists for the given header(s), for example:
<h3 id="bobcontent1-title" class="handcursor">What is JavaScript?</h3>
<div id="bobcontent1" class="switchgroup1">
<!--Nothing here-->
</div>
<h3 id="bobcontent2-title" class="handcursor">Difference between Java & JavaScript?</h3>
<div id="bobcontent2" class="switchgroup1">
<!--Nothing here-->
</div>
Then, within your invocation code of Step 2, dynamically assign an external file to the 1st and 2nd headers above, by calling the setContent() function:
<script type="text/javascript">
var bobexample=new switchcontent("switchgroup1", "div") //Limit scanning of switch contents to just "div" elements
"
"
bobexample.setContent(0, 'whatisjavascript.htm') //specify remote content for 1st header's content
bobexample.setContent(1, 'whatisjava.htm') //specify remote content for 2nd header's content
bobexample.init()
</script>
And there you have it.
Here's a detailed explanation of what each line does, some of which are optional:
| Method | Description |
|---|---|
new switchcontent("classname",
Required |
Main Switch Content object function that sets up contents with a shared CSS class name to be switch contents.
Parameters:
|
instance.setStatus(openHTML, closedHTML) |
Sets HTML that's added in front of each header to indicate its content's expand/contract status.
Parameters:
|
instance.setColor(openheaderColor, closedheaderColor) |
Sets the header's color (if it's text based) depending on whether its content is expanded or contracted.
Parameters:
|
instance.setContent(index, filepath) New |
Allows you to specify an external file as the corresponding content for a header. Call this function multiple times to specify Ajax content for multiple headers.
Parameters:
The remote content is only fetched when the header is expanded, and only once. So if all your headers are Ajax based and contracted by default, none of the remote contents are fetched until the header in question is expanded. Contracting then expanding the same header again won't cause the content to be fetched again. Examples:
|
instance.setPersist(Boolean, [days]) |
Enables or disables persistence, so the state of the contents is remembered either for the browser session or x days. A browser session is defined as until the user closes the browser.
Parameters:
Examples:
|
instance.collapsePrevious(Boolean) |
Specifies whether any previously expanded content should be contracted first before expanding the current one, effectively specifying that only one content can be open at any time.
Parameter:
|
instance.defaultExpanded(indices) |
By default, the script will contract all contents within a switch group when the page first loads. Use this method to specify those contents that should be expanded by default, by entering their index (position) relative to the other contents inside the group, each separated by a comma (,).
Parameter: Indices of the contents to be expanded by default relative to the rest of the contents (starts at 0). A few examples:
Exceptions: Two conditions if met will override this setting:
|
instance.init()
Required |
Call this function at the very end to initialize the Switch Content group using the above settings. |
And just for reinforcement, here's the JavaScript invocation code for the 2nd demo you see at the beginning of the page:
<script type="text/javascript">
var joeexample=new switchcontent("switchgroup2", "p")
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) //1st and 2nd contents within group should be expanded by default
joeexample.setContent(0, 'whatisjavascript.htm') //specify remote content for 1st header's content
joeexample.setContent(1, 'whatisjava.htm') //specify remote content for 2nd header's content
joeexample.init()
</script>
Have fun! But before we go, if you want to define manual links on the page to instantly contract or expand all contents within a Switch group, use the below method within your HTML code:
instance.sweepToggle('contract/expand') |
Contracts or expands all contents within a Switch group.
Parameter: "contract" or "expand". Example: <div> |
Ok now we're really done. :-)