实现记住我的帐号的功能

23 篇文章 0 订阅
15 篇文章 0 订阅

/**************************************************
参数说明:

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;
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值