PDA

View Full Version : please help to modify cookie



eidalina20
08-08-2006, 04:32 PM
please help to modify cookie

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

hello everyone... i really need someone to modify this for me...
cookie that i have there remembers which menu block the user is using.
what i am trying to fix is:
- when user clicks on any link within the block i want that link to be highlighted and stay highlighted after page refresh and then have white background if other link is in use. So basically only active link to be highlighted and stay highlighted for user to see where he is....
any help will be appreciated.. thank you

my code:

<script type="text/javascript">

var persistmenu="yes"
var persisttype="sitewide"

if (document.getElementById){
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("ol");
if(el.style.display != "block"){
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu"){
ar[i].style.display = "none";
}
}
el.style.display = "block";
}
}
}

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>



i thought of using this function for highlightnig:

function highlight(which,color) {
var rows=document.getElementsByTagName("li");
for(i=0;i<rows.length;i++) {
rows[i].style.backgroundColor="white";
}
which.style.background=color;
}


and here is the piece of my HTML menu

<div id="masterdiv">
<table width="100%">
<tr>
<td class="title"><b>Home:</b><a href="javascript: void(0);" onclick="sub1.style.display='none';" style="text-decoration: none">&nbsp;[-]</a><a href="javascript: void(0);" onclick="SwitchMenu('sub1')" style="text-decoration: none">[+]</a></td></tr></table>
<cfoutput><table width="100%">
<cfset lnk_path = "#application.projectDir#/tools/training/web/test_index.cfm?">
<tr valign="top">
<td>
<ol class="submenu" id="sub1">
<li><a href="#lnk_path#fuse=home_welcome" onclick="highlight(this);">Welcome</a><br>
<li><a href="#lnk_path#fuse=home_overview" onclick="highlight(this);">Overview</a><br>
<li><a href="#lnk_path#fuse=home_mission" onclick="highlight(this);">Mission</a><br>
<li><a href="#lnk_path#fuse=home_prerequisites" onclick="highlight(this);">Prerequisites</a><br>
<li><a href="#lnk_path#fuse=courses" onclick="highlight(this);">Computer Based Training</a>
</ol>
</td>
</tr></table></cfoutput>

so basically the onload and save functions should be changed...... thank you