PDA

View Full Version : Auto display specific Switch Menu subs upon opening



mdivrx
02-27-2006, 07:20 PM
Switch Menu (http://www.dynamicdrive.com/dynamicindex1/switchmenu.htm)

Here is what I am trying to do. Here is my tree...

a
...1
...2
...3
b
...1
...2
...3
c
...1
...2
...3

When "a1" loads for the first time, I want the "a" submenu to automatically display. When "b1" loads for the first time, I want the "b" submenu to automatically display. When "c1" loads for the first time, I want the "c" submenu to automatically display.

Right now my opening page has links to a1, b1, and c1. So, when I click on those links, I want the corresponding submenus to automatically display.

I want to continue using persistance sitewide.

What modification can you suggest?

Thanks!

jscheuer1
02-28-2006, 01:58 PM
Find this function in the script:


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"
else
document.getElementById('sub1').style.display="block"
}
}

Add the part in red. Use the desired id (in green) for each page.

Crimsonbat
03-20-2006, 08:05 PM
By using your proposed solution jscheuer1, sub1 is always open even if you close it and browse to another page (eliminates sitewide persistence for sub1). Is it possible to make it so that if you close sub1, it also has persistence and keeps it closed/open with sitewide persistence? Anyone got a solution?

Crimsonbat
03-20-2006, 09:31 PM
please help!!

jscheuer1
03-20-2006, 09:47 PM
That's how my modification does function, are you sure you have persistence turned on? The only exception being that if all items are closed, sub1 again becomes the default. Additionally, with this setup, you need not use sub1 on each page that has the script, then only the sub# on each page would have precedence if none were selected previously.

jscheuer1
03-20-2006, 10:53 PM
If you want to go to the point where once the menu is run, all bets are off as it were with persistence overriding all previous script settings in a sitewide install, use this script (set the sub# you want initially in the two spots highlighted red in onloadfunction() ):


<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
* Modified here to have a default menu open if first time
* run by jscheuer1 in http://www.dynamicdrive.com/forums
***********************************************/

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"
else if (get_cookie(cookiename+'2')!=='run'){
document.getElementById('sub1').style.display="block"
}
}
else
document.getElementById('sub1').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
document.cookie=cookiename+"2=run"+(persisttype=="sitewide"?";path=/":"")
}

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>