是否有 JavaScript 库或轻松使用 cookie?
user2847819
问问题
8500 次
1 回答
20
香草 javascript FTW
/*********************************************************
gets the value of a cookie
**********************************************************/
document.getCookie = function(sName)
{
sName = sName.toLowerCase();
var oCrumbles = document.cookie.split(';');
for(var i=0; i<oCrumbles.length;i++)
{
var oPair= oCrumbles[i].split('=');
var sKey = decodeURIComponent(oPair[0].trim().toLowerCase());
var sValue = oPair.length>1?oPair[1]:'';
if(sKey == sName)
return decodeURIComponent(sValue);
}
return '';
}
/*********************************************************
sets the value of a cookie
**********************************************************/
document.setCookie = function(sName,sValue)
{
var oDate = new Date();
oDate.setYear(oDate.getFullYear()+1);
var sCookie = encodeURIComponent(sName) + '=' + encodeURIComponent(sValue) + ';expires=' + oDate.toGMTString() + ';path=/';
document.cookie= sCookie;
}
/*********************************************************
removes the value of a cookie
**********************************************************/
document.clearCookie = function(sName)
{
setCookie(sName,'');
}
于 2013-10-04T20:04:52.000 回答