js对Cookie的读写操作

//读Cookie
function getCookie(objName) {//获取指定名称的cookie的值
    var arrStr = document.cookie.split("; ");
    for (var i = 0; i < arrStr.length; i++) {
        var temp = arrStr[i].split("=");
        if (temp[0] == objName) return unescape(temp[1]);
    }
    return "";
}
//设置cookie的值
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    console.log(cname + '/' + cvalue + '/' + exdays)
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + escape(cvalue) + "; " + expires;
    console.log(document.cookie);
}

 

posted @ 2016-10-12 17:57  方金  阅读(5793)  评论(0编辑  收藏  举报