spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / css_content / 1 To page 1To page 2current page
[previous]

Expandable/Collapsible Content

Java Software Engineer / Architect Sr (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


Persistence

Under Internet Explorer 5 and later, it's possible to save the current session's expand/collapse state. Persistence is achieved through a combination of a meta tag and style rule, and lasts for the lifetime of the browser window. For all the details, visit Microsoft's MSDN library.

Meta Tag

Add the following to the head section of any page where you want persistence:

<meta name="save" content="history" />

Style Rule

Add the following definition, then apply as needed:

.save{
   behavior:url(#default#savehistory);}
...

<div class="save">
<h1><a href="javascript:void(0)" class="dsphead"
   onclick="dsp(this)">
   <span class="dspchar">+</span> heading</a></h1>
   <div class="dspcont">section</div>
...

</div>

It's best to reserve the save class for sections of expand/collapse content, and not apply it to the entire page. That's because any updates to dynamic content, or even edits during development, won't be displayed until the browser window is closed and reopened.

Complete Code

To help you get started with expandable content, here's complete, copy and paste code for the example we've been building. Multiply sections as needed, fill them in with real content, and watch how cumbersome slabs of text become nimble and easy on the eye.

<html><head>
<title></title>

<meta name="save" content="history" />

<style type="text/css"><!--
.save{
   behavior:url(#default#savehistory);}
a.dsphead{
   text-decoration:none;
   margin-left:1.5em;}
a.dsphead:hover{
   text-decoration:underline;}
a.dsphead span.dspchar{
   font-family:monospace;
   font-weight:normal;}
.dspcont{
   display:none;
   margin-left:1.5em;}
//--></style>


<script type="text/javascript"><!--
function dsp(loc){
   if(document.getElementById){
      var foc=loc.firstChild;
      foc=loc.firstChild.innerHTML?
         loc.firstChild:
         loc.firstChild.nextSibling;
      foc.innerHTML=foc.innerHTML=='+'?'-':'+';
      foc=loc.parentNode.nextSibling.style?
         loc.parentNode.nextSibling:
         loc.parentNode.nextSibling.nextSibling;
      foc.style.display=foc.style.display=='block'?'none':'block';}}  

if(!document.getElementById)
   document.write('<style type="text/css"><!--\n'+
      '.dspcont{display:block;}\n'+
      '//--></style>');
//--></script>

<noscript>
<style type="text/css"><!--
.dspcont{display:block;}
//--></style>
</noscript>

</head><body>

<div class="save">

<h1><a href="javascript:void(0)" class="dsphead"
   onclick="dsp(this)">
   <span class="dspchar">+</span> heading</a></h1>
   <div class="dspcont">section</div>

<h1><a href="javascript:void(0)" class="dsphead"
   onclick="dsp(this)">
   <span class="dspchar">+</span> heading</a></h1>
   <div class="dspcont">

      <h2><a href="javascript:void(0)" class="dsphead"
         onclick="dsp(this)">
         <span class="dsphead">+</span> heading</a></h2>
         <div class="dspcont">section</div>

      <h2><a href="javascript:void(0)" class="dsphead"
         onclick="dsp(this)">
         <span class="dspchar">+</span> heading</a></h2>
         <div class="dspcont">section</div>
   </div>
</div>

</body></html>

About the Author

M. C. Matti is a usability analyst at SAS, working on Web-based business intelligence applications. He's also the owner of matti.net. Send him a note at mimatt@sas.com.


home / programming / css_content / 1 To page 1To page 2current page
[previous]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

Created: March 27, 2003
Revised: June 26, 2003

URL: http://webreference.com/programming/css_content/1