Cookie添加、获取以及删除操作

/**
 * 添加cookie值
 */
function setCookie (name, value) {  
    document.cookie = name + "=" + escape(value); 
} 
 
/**
 * 根据cookie名,取得cookie值
 */ 
function getCookie(name) {   
    var search;  
    search = name + "=" 
    offset = document.cookie.indexOf(search) 
    if (offset != -1) { 
        offset += search.length ; 
        end = document.cookie.indexOf(";", offset) ; 
        if (end == -1) {
            end = document.cookie.length;
        }
        return unescape(document.cookie.substring(offset, end)); 
    } else {
        return "";
    } 
    
} 

/**
 * 删除某一cookie 
 */
function deleteCookie(name) {
    //此 cookie 将被保存 30 天
    var Days = 30;
    var expdate = new Date(); 
    expdate.setTime(expdate.getTime() - (Days*24*60*60*1000)); 
    setCookie(name, "", expdate); 
}

猜你喜欢

转载自blog.csdn.net/weixin_42273374/article/details/80430462