JS 的读写 Cookie
作者:郑凯
var cookie_name = "COOKIE 变量名";
var cookie_content = "COOKIE 内容";
写比较简单,主要是 toGMTString,而且通过这个才知道 js 提供的时间戳的单位是微秒,faint
var tcookie_time = new Date();
var tcookie_expiry = new Date(tcookie_time.getTime() + 1000 * 3600 * 24 * 90);
document.cookie = cookie_name "=" + cookie_content + "; expires=" + tcookie_expiry.toGMTString();
读比较麻烦,因为得到的所有 COOKIE 是一个整个的字符串,要切出来
[code]
function getCookie(sCookieName) {
var sSearch = sCookieName + "=";
var sReturn = "";
var sCookie = document.cookie;
if (sCookie.length > 0) {
iOffset = sCookie.indexOf(sSearch);
if (iOffset != -1) {
iOffset += sSearch.length
iEnd = sCookie.indexOf(";", iOffset);
if (iEnd == -1) {
iEnd = sCookie.length;
}
sReturn = decodeURIComponent(sCookie.substring(iOffset, iEnd));
}
}
return sReturn;
}
[/code]