trinzia
09-01-2006, 06:08 AM
Hi,
I am a novice, trying to make a menu script. It is a simple 2-level hierarchy, using a query string to keep open the branch which was clicked. It also adds a style the selected page link (red).
If you save this html as page1 and page2, you can see, that the menu will close all open branches when you click a new one. This is what I wanted. But, sometimes it does not... sometimes 2 branches can be open at the same time. I need it to have only 1 branch open at a time.
Please understand that I can barely write one line of code without breaking it all... I didn't write the menuOPen function. I can understand the problem is that I have assigned 2 items the "menuopen" state, but any time I touch the script at this point, I break everything. OH and there's an error in the script too but I don't know what it is.
Thanks for any help.
Trinzia
<html>
<head>
<title>persist menu</title>
<style type="text/css">
div.menuclosed div.sublinks { display: none; }
div.menuopen {display:visible;}
.toplink { display:block; padding:3px 2px 3px 5px;
text-decoration:none; color:#006099;
border-top: 1px solid #cccccc;}
.sublinks a { display:block; padding:2px 2px 5px 26px; text-decoration:none;}
.sublinks a:link { color:#006099;}
.sublinks a:visited { color:#006099;}
.sublinks a:hover { color:#996000; background:#eeeeee;}
.selected {color:#ff2222 !important;}
</style>
<script language="javascript" type="text/javascript">
var theDiv;
var selectParent;
function checkMenu(){
var result = location.search.split("=");
var selected = document.getElementById(result[1]);
selected.className="selected";
selectParent = selected.parentNode.parentNode;
selectParent.className = "menuopen";
}
function openMenu(toplink) {
if(theDiv){
theDiv.className = "menuclosed";
if(theDiv==toplink.parentNode){
theDiv = null;
return false;
}
}
theDiv = toplink.parentNode;
theDiv.className = "menuopen";
}
</script>
</head>
<body onLoad="checkMenu()">
<div class="menuclosed" id="a">
<a class="toplink" href="#" onclick="openMenu(this); return false;">Branch A</a>
<div class="sublinks">
<a href="page1.html?menuItem=a1" id="a1">Link Page a1</a>
<a href="page2.html?menuItem=a2" id="a2">Link Page a2</a>
</div>
</div>
<div class="menuclosed" id="b">
<a class="toplink" href="#" onclick="openMenu(this); return false;">Branch B</a>
<div class="sublinks">
<a href="page1.html?menuItem=b1" id="b1">Link Page b1</a>
<a href="page2.html?menuItem=b2" id="b2">Link Page b2</a>
</div>
</div>
</body>
</html>
I am a novice, trying to make a menu script. It is a simple 2-level hierarchy, using a query string to keep open the branch which was clicked. It also adds a style the selected page link (red).
If you save this html as page1 and page2, you can see, that the menu will close all open branches when you click a new one. This is what I wanted. But, sometimes it does not... sometimes 2 branches can be open at the same time. I need it to have only 1 branch open at a time.
Please understand that I can barely write one line of code without breaking it all... I didn't write the menuOPen function. I can understand the problem is that I have assigned 2 items the "menuopen" state, but any time I touch the script at this point, I break everything. OH and there's an error in the script too but I don't know what it is.
Thanks for any help.
Trinzia
<html>
<head>
<title>persist menu</title>
<style type="text/css">
div.menuclosed div.sublinks { display: none; }
div.menuopen {display:visible;}
.toplink { display:block; padding:3px 2px 3px 5px;
text-decoration:none; color:#006099;
border-top: 1px solid #cccccc;}
.sublinks a { display:block; padding:2px 2px 5px 26px; text-decoration:none;}
.sublinks a:link { color:#006099;}
.sublinks a:visited { color:#006099;}
.sublinks a:hover { color:#996000; background:#eeeeee;}
.selected {color:#ff2222 !important;}
</style>
<script language="javascript" type="text/javascript">
var theDiv;
var selectParent;
function checkMenu(){
var result = location.search.split("=");
var selected = document.getElementById(result[1]);
selected.className="selected";
selectParent = selected.parentNode.parentNode;
selectParent.className = "menuopen";
}
function openMenu(toplink) {
if(theDiv){
theDiv.className = "menuclosed";
if(theDiv==toplink.parentNode){
theDiv = null;
return false;
}
}
theDiv = toplink.parentNode;
theDiv.className = "menuopen";
}
</script>
</head>
<body onLoad="checkMenu()">
<div class="menuclosed" id="a">
<a class="toplink" href="#" onclick="openMenu(this); return false;">Branch A</a>
<div class="sublinks">
<a href="page1.html?menuItem=a1" id="a1">Link Page a1</a>
<a href="page2.html?menuItem=a2" id="a2">Link Page a2</a>
</div>
</div>
<div class="menuclosed" id="b">
<a class="toplink" href="#" onclick="openMenu(this); return false;">Branch B</a>
<div class="sublinks">
<a href="page1.html?menuItem=b1" id="b1">Link Page b1</a>
<a href="page2.html?menuItem=b2" id="b2">Link Page b2</a>
</div>
</div>
</body>
</html>