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

/**

*设置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;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容