首页 > Web开发 > 详细

如何使用原生 js 处理 cookie

时间:2014-04-20 21:39:13      阅读:522      评论:0      收藏:0      [点我收藏+]

卓越网的代码,参考学习。

bubuko.com,布布扣
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function deleteCookie(cookie_name) {
    var cookie_date = new Date(); // current date & time
    cookie_date.setTime(cookie_date.getTime() - 1);
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function checkCookieEnabled(nodeId) {
    setCookie(‘amznTest‘, ‘1‘, null);
    if (getCookie(‘amznTest‘)) {
        deleteCookie(‘amznTest‘);
    } else {
        document.getElementById(nodeId).style.display = ‘block‘;
    }
}
checkCookieEnabled(‘message_warning‘);
bubuko.com,布布扣

 

如何使用原生 js 处理 cookie,布布扣,bubuko.com

如何使用原生 js 处理 cookie

原文:http://www.cnblogs.com/shrekuu/p/3676527.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!