【问题标题】:Is there a good 'cookie' library for javascript? [closed]是否有一个好的 'cookie' 库用于 javascript? [关闭]
【发布时间】:2014-02-26 19:30:56
【问题描述】:

是否有 JavaScript 库或可以轻松使用 cookie?

【问题讨论】:

标签: javascript cookies


【解决方案1】:

原版 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,'');
}

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 2011-02-09
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2011-04-15
    • 2010-11-30
    • 2011-06-17
    相关资源
    最近更新 更多