js 获取读取cookie

// --- 设置cookie
function setCookie(sName, sValue, expireHours) {
    var cookieString = sName + "=" + escape(sValue);
    //;推断是否设置过期时间
    if (expireHours > 0) {
        var date = new Date();
        date.setTime(date.getTime + expireHours * 3600 * 1000);
        cookieString = cookieString + "; expire=" + date.toGMTString();
    }
    document.cookie = cookieString;
}

//--- 获取cookie
function getCookie(sName) {
    var aCookie = document.cookie.split("; ");
    for (var j = 0; j < aCookie.length; j++) {
        var aCrumb = aCookie[j].split("=");
        if (escape(sName) == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    return null;
}

 

posted @ 2017-04-14 09:57  zhchoutai  阅读(1002)  评论(0编辑  收藏  举报