PDA

View Full Version : Switch Menu - Highlighting Submenu



jolly_jonesy
04-03-2007, 11:07 PM
1) Script Title: Switch Menu

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex1/switchmenu.htm

3) Describe problem:

My Problem is that I have been trying and trying to find a way to keep a submenu item highlighted whilst on the page it links to - Please Please Please Help.

The website I have developed can be seen at:

www.flowmaster.com

The switch enu functions really well but this is very much needed and I am bashing my head against a brick wall and have been for months now... have submitted a few times with no resolve Please help someone!

Steve

codeexploiter
04-04-2007, 05:05 AM
You can try something like the following



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<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;
}

.test
{
border:1px solid #000;
padding:2px 2px 2px 2px;
background-color:#A6CAF0
}
</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 globalObj = "";
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



function callIt(obj)
{
if(globalObj != "")
{
globalObj.className = "";
}
obj.className = "test";
globalObj = obj;
return;

}
</script>
</head>

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

<div class="menutitle" onclick="SwitchMenu('sub1')">Site Menu</div>
<span class="submenu" id="sub1">
- <a href="#" onclick="callIt(this);">What's New</a><br>
- <a href="#" onclick="callIt(this);">What's hot</a><br>
- <a href="#" onclick="callIt(this);">Revised Scripts</a><br>
- <a href="#" onclick="callIt(this);">More Zone</a>
</span>

<div class="menutitle" onclick="SwitchMenu('sub2')">FAQ/Help</div>
<span class="submenu" id="sub2">
- <a href="#" onclick="callIt(this);">Usage Terms</a><br>
- <a href="#" onclick="callIt(this);">DHTML FAQs</a><br>
- <a href="#" onclick="callIt(this);">Scripts FAQs</a>
</span>

<div class="menutitle" onclick="SwitchMenu('sub3')">Help Forum</div>
<span class="submenu" id="sub3">
- <a href="#" onclick="callIt(this);">Coding Forums</a><br>
</span>

<div class="menutitle" onclick="SwitchMenu('sub4')">Cool Links</div>
<span class="submenu" id="sub4">
- <a href="#" onclick="callIt(this);">JavaScript Kit</a><br>
- <a href="#" onclick="callIt(this);">Freewarejava</a><br>
- <a href="#" onclick="callIt(this);">Cool Text</a><br>
- <a href="#" onclick="callIt(this);">Google.com</a>
</span>

</div>
</body>
</html>

jolly_jonesy
04-04-2007, 09:18 AM
Thanks for the info I'll have a try at implimenting it.. One question before I do however. Do I copy this code for code or am I meant to insert variable elements myself?.. and if so where.. I was guessing between the "" for example:

var globalObj = "";

Any further clarification would be appreciated .

cheers

Steve