美文网首页js
设置cookie,删除cookie,读取cookie值

设置cookie,删除cookie,读取cookie值

作者: 悠哉悠哉过日子 | 来源:发表于2017-11-15 16:12 被阅读15次

/**

*设置cookie

*/

function setCookie(cookieName, cookieValue) {

    var date = newDate();

    var expiresDays = 1;

    date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000);// cookie失效时间

    document.cookie = cookieName +'='+ escape(cookieValue) +";expires="+     date.toGMTString()+";path=/";//+";path=/"

}

/**

*删除cookie

*/

function DelCookie(name) {

    var exp = newDate();

    exp.setTime(exp.getTime() + (-1 * 24 * 60 * 60 * 1000));

    var cval = getCookieValueByName(name);

    document.cookie = name +"="+ cval +"; expires="+ exp.toGMTString()+";path=/";

}

/**

*读取cookie值

*/

function getCookieValueByName(cookieName) {

    var cookieValue = null;

    var begin = document.cookie.indexOf(cookieName);

    if(begin != -1) {

        begin += cookieName.length + 1;

        var end = document.cookie.indexOf(";", begin);

        if(end == -1) {

            end = document.cookie.length;

        }

        cookieValue = unescape(document.cookie.substring(begin, end));// 获取cookieName的值

    }

    return cookieValue;

}

相关文章

网友评论

    本文标题:设置cookie,删除cookie,读取cookie值

    本文链接:https://www.haomeiwen.com/subject/zjchvxtx.html