Cookie 操作
<script type="text/javascript" language="javascript">
<!--
//写入 cookie
//不区分 cookieName 的大小写
//不考虑子键
function ResponseCookies(cookieName, cookieValue, expires)
{
if (expires)
{
//指定了 expires
document.cookie = ResponseCookies_GetCookieName(cookieName) + "=" + escape(cookieValue) + "; expires=" + expires.toGMTString();
}
else
{
document.cookie = ResponseCookies_GetCookieName(cookieName) + "=" + escape(cookieValue);
}
}
//获取并返回与 cookieName 同名的 cookie 名称,允许大小写不同
//如果不存在这样的 cookie,就返回 cookieName
function ResponseCookies_GetCookieName(cookieName)
{
var lowerCookieName = cookieName.toLowerCase();
var cookieStr = document.cookie;
if (cookieStr == "")
{
return cookieName;
}
var cookieArr = cookieStr.split("; ");
var pos = -1;
for (var i=0; i<cookieArr.length; i++)
{
pos = cookieArr[i].indexOf("=");
if (pos > 0)
{
if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName)
{
return cookieArr[i].substring(0, pos);
}
}
}
return cookieName;
}
//获取并返回 cookie 值
//不区分 cookieName 的大小写
//dfltValue 为默认返回值
//不考虑子键
function RequestCookies(cookieName, dfltValue)
{
var lowerCookieName = cookieName.toLowerCase();
var cookieStr = document.cookie;
if (cookieStr == "")
{
return dfltValue;
}
var cookieArr = cookieStr.split("; ");
var pos = -1;
for (var i=0; i<cookieArr.length; i++)
{
pos = cookieArr[i].indexOf("=");
if (pos > 0)
{
if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName)
{
return unescape(cookieArr[i].substring(pos+1, cookieArr[i].length));
}
}
}
return dfltValue;
}
document.write("写入名称为 cv 的 cookie...<br>");
ResponseCookies("cv", "cv20", null);
document.write("写入名称为 Ab,带失效日期的 cookie...<br>");
var expires = new Date("December 31, 2045");
ResponseCookies("Ab", "Ab20", expires);
document.write("读取名称为 ab 的 cookie..." + RequestCookies("ab", ""));
-->
</script>
****************************************************************************
function GetCookieVal(offset)
//获得Cookie解码后的值
{
var endstr = documents.cookie.indexOf(";", offset);
if (endstr == -1)
endstr = documents.cookie.length;
return unescape(documents.cookie.substring(offset, endstr));
}
function SetCookie(name, value)
//设定Cookie值
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
documents.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expdate.toGMTString()))
+ ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain))
+ ((secure == true) ? "; secure" : "");
}
function DelCookie(name)
//删除Cookie
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = GetCookie(name);
documents.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
function GetCookie(name)
//获得Cookie的原始值
{
var arg = name + "=";
var alen = arg.length;
var clen = documents.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (documents.cookie.substring(i, j) == arg)
return GetCookieVal(j);
i = documents.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
******************************************************************
//写cookies函数 作者:翟振凯
function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
{
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}
function delCookie(name)//删除cookie
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}