存cookie
//保存中文信息到Cookie, 要使用UrlEncode編碼
var cookie = new HttpCookie("info");
cookie.Value = Server.UrlEncode("中文信息-" + DateTime.Now.ToString());
cookie.Expires = DateTime.Now.AddMinutes(1); //只保留1分鐘
Response.SetCookie(cookie);//必須寫(xiě)不然cookie沒(méi)保存
//過(guò)期時(shí)間改到5天之前浸间,立即過(guò)期,因此也清除此cookie的方法
cookie.Expires = DateTime.Now.AddDays(-5);
取cookie
HttpCookie cookie = Request.Cookies["info"]; //根據(jù)名稱直接查找cookie旁涤、
localStorage
if (!window.localStorage) {
? ? ? ? ? ? ? ? alert("瀏覽器不支持localstorage");
? ? ? ? ? ? ? ? return false;
}
3中寫(xiě)入方法
(可以不new? var storage = window.localStorage;)
//寫(xiě)入a字段
localStorage["a"] = 6;
//寫(xiě)入b字段
localStorage.b = 7;
//寫(xiě)入c字段
localStorage.setItem("c", 8);
簡(jiǎn)單的例子:
localStorage.setItem(“name”,“panda”);//設(shè)置name為panda
localStorage.setItem(“name”,“Jane”);//覆蓋之前的值队贱,現(xiàn)在name所對(duì)應(yīng)的值是Jane
localStorage.getItem(“name”);//獲取name的值芳绩,即Jane
localStorage.removeItem(“name”);//刪除name以及name的值
localStorage.clear();//清除localstorage里邊所有數(shù)據(jù)
function forEachKey(callback) {//遍歷顯示localStorage中的key
? for (var i = 0; i < localStorage.length; i++) {
? ? callback(localStorage.key(i));
? }
}