PDA

View Full Version : Leaving more than one menu open on Switch Menu with cookie



Heavens_Candy
04-25-2005, 06:50 AM
Script: DD Switch Menu
http://www.dynamicdrive.com/dynamicindex1/switchmenu.htm

Hi, I hope you can help me out with this and I am explaining it correctly.. :eek: I am using the DD Switch Menu, but I would like to be able to click on a menu, it drops down. Then I click on another one and it drops down while the other one stays open...and so on for the rest of the menus. If you click on a menu that is already open just that one closes. Now I figured that out buy just putting in this code below:

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

el.style.display = "block";
}else{
el.style.display = "none";
}
}
}

</script>

BUT Now there is no cookie set...so if I go to another page and then go back to that page all of them are closed again. :( I want the ones I have left dropped down to still be in that state upon going back to that page.

Thanks for any help!

jscheuer1
04-25-2005, 08:14 AM
Add this after your code but, before the closing </script> tag:
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!="")
if (cookievalue!="[]"){
openids=new Array()
openids=eval(cookievalue)
for (i = 0; i < openids.length; i++){
if (openids[i]!==undefined)
document.getElementById(openids[i]).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++
}
blockid+="]"
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

Heavens_Candy
04-25-2005, 07:44 PM
Dude you rock! Thank you so much you made my life easier..hehe :)