Ubuntu / Linux news and application reviews.

Demo:



The menu uses a cookie to remember it's last state. Click on a category in the menu to expand it and then reload the page. The same category should expand upon page reload. You can choose between "sitewide" and "local" persistence, with the former remembering the menu state across your entire site. This is useful if you have the exact same Switch Menu script on multiple pages on your site (ie: as a navigational bar), and it makes sense for the persistence to last from page to page.

CSS and JavaScript (place this code above the </head> tag in your template:

<style type="text/css">
.menutitle{
cursor:pointer;
margin-bottom: 5px;
background-color:#ECECFF;
color:#000000;
width:140px;
padding:2px;
text-align:center;
font-weight:bold;
/*/*/border:1px solid #000000;/* */
}

.submenu{
margin-bottom: 0.5em;
}
</style>

<script type="text/javascript">

/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="sitewide" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}

function SwitchMenu(obj){
if(document.getElementById){
var el = document.getElementById(obj);
var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
if(el.style.display != "block"){ //DynamicDrive.com change
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu") //DynamicDrive.com change
ar[i].style.display = "none";
}
el.style.display = "block";
}else{
el.style.display = "none";
}
}
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function onloadfunction(){
if (persistmenu=="yes"){
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=get_cookie(cookiename)
if (cookievalue!="")
document.getElementById(cookievalue).style.display="block"
}
}

function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener)
window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate

</script>


HTML (Add the below where you want the menu to appear on your page):

<!-- Keep all menus within masterdiv-->
<div id="masterdiv">

 <div class="menutitle" onclick="SwitchMenu('sub1')">Site Menu</div>
 <span class="submenu" id="sub1">
  - <a href="new.htm">What's New</a><br>
  - <a href="hot.htm">What's hot</a><br>
  - <a href="revised.htm">Revised Scripts</a><br>
  - <a href="morezone/">More Zone</a>
 </span>

 <div class="menutitle" onclick="SwitchMenu('sub2')">FAQ/Help</div>
 <span class="submenu" id="sub2">
  - <a href="notice.htm">Usage Terms</a><br>
  - <a href="faqs.htm">DHTML FAQs</a><br>
  - <a href="help.htm">Scripts FAQs</a>
 </span>

 <div class="menutitle" onclick="SwitchMenu('sub3')">Help Forum</div>
 <span class="submenu" id="sub3">
  - <a href="codingforum.htm">Coding Forums</a><br>
 </span>
 
 <div class="menutitle" onclick="SwitchMenu('sub4')">Cool Links</div>
 <span class="submenu" id="sub4">
  - <a href="javascriptkit.htm">JavaScript Kit</a><br>
  - <a href="freewarejava.htm">Freewarejava</a><br>
  - <a href="cooltext.htm">Cool Text</a><br>
  - <a href="http://www.google.com">Google.com</a>
 </span>

 <img src="http://www.netupd8.com/w8img/17ac8n.jpg" onclick="SwitchMenu('sub5')"><br>
 <span class="submenu" id="sub5">
  - <a href="link.htm">Link to DD</a><br>
  - <a href="recommendit.htm">Recommend Us</a><br>
  - <a href="contact.html">Email Us</a><br>
 </span>

</div>

via: makeuseof / credits: dynamicdrive