Pawn Posted June 16, 2008 Share Posted June 16, 2008 Sorry if this is the wrong place for this. I just added some expandable bits to my site over at superlatenight.com. I believe they display more or less correctly (hidden by default) in most browsers (correct me if I'm wrong). However, I would like to maintain the display state, so that expanded sections remain shown after refresh. Is there a simple way of doing this, or should I use a session variable? Quote Link to comment Share on other sites More sharing options...
xtopolis Posted June 16, 2008 Share Posted June 16, 2008 Cookies or sessions. You could probably use javascript for it. PHP sessions might be a bit much just for saving display states, but, still a valid option. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 Here is a pure JavaScript example I whipped up...hope it helps: <html> <head> <script type="text/javascript"> function toggle ( eleId ) { var ele = document.getElementById(eleId); return setShowing(eleId,(ele.style.display == 'none')); } function setShowing ( eleId, showing ) { var ele = document.getElementById(eleId); ele.style.display = showing ? '' : 'none'; var c_name = 'state_'+eleId; showing = showing ? 1 : 0; document.cookie=c_name+ "=" +showing; return true; } function initState ( eleId ) { var c_name = 'state_'+eleId; var showing = 0; if(document.cookie.length>0){ c_start=document.cookie.indexOf(c_name + "="); if(c_start!=-1){ c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if(c_end==-1) c_end=document.cookie.length; showing = unescape(document.cookie.substring(c_start,c_end)); } } showing = (showing == 1) ? true : false; return setShowing(eleId,showing); } window.onload = function ( ) { initState('test'); } </script> </head> <body> <span onclick="toggle('test');">Click to Toggle</span> <div id="test" style="display:none;width:100px;height:100px;background:red;">Hello</div> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.