码迷,mamicode.com
首页 > Web开发 > 详细

js操作获取和设置cookie

时间:2019-09-03 16:08:51      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:turn   fun   read   target   dom   null   expires   var   eva   

//创建cookie
function setCookie(name, value, expires, path, domain, secure) {
    var cookieText = encodeURIComponent(name) + ‘=‘ + encodeURIComponent(value);
    if (expires instanceof Date) {
        cookieText += ‘; expires=‘ + expires;
    }
    if (path) {
        cookieText += ‘; expires=‘ + expires;
    }
    if (domain) {
        cookieText += ‘; domain=‘ + domain;
    }
    if (secure) {
        cookieText += ‘; secure‘;
    }
    document.cookie = cookieText;
}
 
//获取cookie
function getCookie(name) {
    var cookieName = encodeURIComponent(name) + ‘=‘;
    var cookieStart = document.cookie.indexOf(cookieName);
    var cookieValue = null;
    if (cookieStart > -1) {
        var cookieEnd = document.cookie.indexOf(‘;‘, cookieStart);
        if (cookieEnd == -1) {
            cookieEnd = document.cookie.length;
        }
        cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
    }
    return cookieValue;
}
 
//删除cookie
function unsetCookie(name) {
    document.cookie = name + "= ; expires=" + new Date(0);
}

原文地址:https://github.com/hawx1993/Front-end-Interview-questions

js操作获取和设置cookie

标签:turn   fun   read   target   dom   null   expires   var   eva   

原文地址:https://www.cnblogs.com/wujhblog/p/11453549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!