/**************************************************
参数说明:
sMainName Cookie名
sSubName Cookie子键名,留空表示单值Cookie
sSubValue Cookie子键值,单值Cookie时表示Cookie值
nExpires Cookie时效,默认为24小时
sPath Cookie路径
sDomain Cookie作用域
bSecure Cookie是否加密传输
**************************************************/
function SetCookie(sMainName, sSubName, sSubValue, nExpires, sPath, sDomain, bSecure)
{
var nPeriod = ((nExpires) ? nExpires : 24);
var dtCurrTime = new Date();
dtCurrTime.setTime(dtCurrTime.getTime() + nPeriod * 60 * 60 * 1000);
var sCookieName = sMainName + "= ";
var sSubCookieName = ((sSubName) ? (sSubName + "= ") : null);
var sSubCookieValue = escape(sSubValue);
var sWholeCookie = document.cookie;
var sCookie = sCookieName;
if(sSubCookieName) //sSubCookieName存在,为多值Cookie
{
var nValueBegin = sWholeCookie.indexOf(sCookieName);
if(nValueBegin == -1) //没有指定的Cookie
{
sCookie += sSubCookieName + sSubCookieValue;
}
else
{
var nValueEnd = sWholeCookie.indexOf( "; ", nValueBegin);
if(nValueEnd == -1)
nValueEnd = sWholeCookie.length;
var sValue = sWholeCookie.substring(nValueBegin + sCookieName.length, nValueEnd);
var nSubValueBegin = sValue.indexOf(sSubCookieName);
if(nSubValueBegin == -1) //没有指定的子键;
{
//在Cookie值字符串的末尾添加指定的子键
sCookie += sValue + "& " + sSubCookieName + sSubCookieValue;
}
else
{
var nSubValueEnd = sValue.indexOf( "& ", nSubValueBegin);
if(nSubValueEnd == -1)
nSubValueEnd = sValue.length;
//把已有的子键从值字符串中剔除
var sValueRemain = sValue.substring(0, nSubValueBegin) + sValue.substring(nSubValueEnd, sValue.length);
//保证剩下的字符串的开头和结尾都不为 &
if(sValueRemain.substring(0, 1) == "& ")
sValueRemain = sValueRemain.substring(1, sValueRemain.length);
if(sValueRemain.substring(sValueRemain.length - 1, sValueRemain.length) == "& ")
sValueRemain = sValueRemain.substring(0 , sValueRemain.length - 1)
//在值字符串末尾添加指定的子键
if(sValueRemain == " ")
{
sCookie += sSubCookieName + sSubCookieValue;
}
else
{
sCookie += sValueRemain + "& " + sSubCookieName + sSubCookieValue;
}
}
}
}
else //sSubCookieName不存在,为单值Cookie
{
sCookie += sSubCookieValue;
}
sCookie += ";expires= " + dtCurrTime.toGMTString();
sCookie += (sPath) ? ";path= " + sPath : " ";
sCookie += (sDomain) ? ";domain= " + sDomain : " ";
sCookie += (bSecure) ? ";secure " : " ";
document.cookie = sCookie;
}
function GetCookie(sMainName, sSubName)
{
var sCookieName = sMainName + "= ";
var sSubCookieName = (sSubName) ? sSubName + "= " : null;
var sCookie;
var sWholeCookie = document.cookie;
var nValueBegin = sWholeCookie.indexOf(sCookieName);
if(nValueBegin != -1)
{
var nValueEnd = sWholeCookie.indexOf( "; ", nValueBegin);
if (nValueEnd == -1)
nValueEnd = sWholeCookie.length;
var sValue = sWholeCookie.substring(nValueBegin + sCookieName.length, nValueEnd); //获得Cookie值
if(sSubCookieName) //多值Cookie
{
var nSubValueBegin = sValue.indexOf(sSubCookieName);
if(nSubValueBegin != -1)
{
var nSubValueEnd = sValue.indexOf( "& ", nSubValueBegin);
if(nSubValueEnd == -1)
nSubValueEnd = sValue.length;
var sSubValue = sValue.substring(nSubValueBegin + sSubCookieName.length, nSubValueEnd); //获得指定的子键值
return unescape(sSubValue);
}
}
if(!sSubCookieName)
return unescape(sValue);
}
return null;
}
//清除Cookie,目前只能清除指定的单值Cookie或一次性删除多值Cookie
function DeleteCookie(sMainName, sPath, sDomain)
{
var sCookie;
if(CheckCookieExist(sMainName))
{
sCookie = sMainName + "= ";
sCookie += (sPath) ? ";path= " + sPath : " ";
sCookie += (sDomain) ? ";domain= " + sDomain : " ";
sCookie += ";expires=Thu, 01-Jan-70 00:00:01 GMT ";
document.cookie = sCookie;
}
}
实现记住我的帐号的功能
最新推荐文章于 2024-03-21 09:47:07 发布