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

Senior Systems Engineer – Disk-Based Backup/Replication (PA)
Next Step Systems
US-PA-Philadelphia

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


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]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

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

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