如何使用Cookie保存菜单状态?
一、使用Javasceipt操作Cookies的基本方法。
1.写入Cookies。
//メニューのCookiesを新規作成する

functionSetCookie(name,value)...{
//CookieSaveas30Day
varDays=30;
varexp=newDate();
exp.setTime(exp.getTime()+Days*24*60*60*1000);
document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();
}
2.取得Cookies。
//取cookies函数

functiongetCookie(name)...{
vararr=document.cookie.match(newRegExp("(^|)"+name+"=([^;]*)(;|$)"));

if(arr!=null)...{
returnunescape(arr[2]);

}else...{
returnnull;
}
}
3.删除Cookies。
//ユーザーのCookieを削除するe

functiondelCookie(name)...{
varexp=newDate();
exp.setTime(exp.getTime()-1);
varcval=getCookie(name);

if(cval!=null)...{
document.cookie=name+"="+cval+";expires="+exp.toGMTString();
}
}
-----------------------------------------------------------------------------
4.加载menu菜单状态。
//メニューを初期化する

functionMenuInit()...{
varob=document.getElementsByTagName("table");
State=getCookie("Ryouka_Menu");

for(vari=0;i<ob.length;i++)...{
MenuSwitch=State.substring(i-1,i);

if(ob[i].id.substr(0,4)=="KM00"&&MenuSwitch=="0")...{
ob[i].style.display="none";
}else
ob[i].style.display="";
}
}
5.单击菜单时改变Cookieszhong菜单状态。
//クリックする時メニューの状態を変える

functionchangemenu(tablemenu)...{
varcount=newString();
count=tablemenu.id.toString();

if(tablemenu.style.display=="none")...{
tablemenu.style.display="";
//parameter(Bit,スウィッチの状態=Open);

if(Mcookies=="1")...{
ModiCookie(count.substring(4,5),"1");
}

}else...{
tablemenu.style.display="none";
//parameter(Bit,スウィッチの状態=Close);

if(Mcookies=="1")...{
ModiCookie(count.substring(4,5),"0");
}
}
}
6.保存menu状态的具体方法。
//メニュー状態を保存する

functionModiCookie(bit,Value)...{
varState_Front;
varState_Back;
varState;
State=getCookie("Ryouka_Menu");
State_Back=State.substring(bit,State.length);
//alert("State.length="+State.length);
State_Front=State.substring(0,bit-1);
State="";
//alert("State_Front="+State_Front+",Value="+Value+",State_Back="+State_Back);
if(bit==1)
State=State.concat(Value,State_Back);
else
State=State.concat(State_Front,Value,State_Back);
SetCookie("Ryouka_Menu",State);
}
7.注解:
(1)match 方法
使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回。
stringObj.match(rgExp)
(2)unescape 方法
解码用 escape 方法进行了编码的 String 对象。
(3)substr 方法
返回一个从指定位置开始的指定长度的子字符串。
(4)concat 方法(String)
返回字符串值,该值包含了两个或更多个提供的字符串的连接。
string1.concat([string2[, string3[, . . . [, stringN]]]])
(5)substring 方法
返回位于 String 对象中指定位置的子字符串。
strVariable.substring(start, end)
8.客户端Cookies文件内容举例
System_Menu
0110000000
10.10.8.8/rk_cims/
1600
276435712
29801903
411407072
29795868
*
本文地址:
http://www.45fan.com/dnjc/71021.html