首页 > 代码库 > JavaScript Cookie

JavaScript Cookie

1 JS中Cookie的结构:
在JavaScript中,cookie是以键值对的形式保存的,即key=value的格式,各个Cookie之间一般是以分号;分隔

2 示例:

<!DOCTYPE HTML>
<!-- lang: html -->
<html>
<!-- lang: html -->
    <head>
<!-- lang: html -->
        <meta charset=“utf-8”>
<!-- lang: html -->
        <title>Use JS Cookie</title>
<!-- lang: html -->
    </head>
<!-- lang: html -->
    <body>
<!-- lang: html -->
        <script type="text/javascript">
<!-- lang: html -->
        setCookie(‘userName‘, ‘Tom‘, 20);
<!-- lang: html -->
    userName = getCookie(‘userName‘);
<!-- lang: html -->
    alert(‘get cookie userName: ‘+userName);
<!-- lang: html -->
    result = checkCookie(‘userName‘) ? ‘Yes‘ : ‘No‘;
<!-- lang: html -->
    alert(‘checke cookie userName is exist?  ‘+result);
<!-- lang: html -->

<!-- lang: html -->
    delCookie(‘userName‘);
<!-- lang: html -->
    alert(‘has deleted cookie userName‘);
<!-- lang: html -->
    userName = getCookie(‘userName‘);
<!-- lang: html -->
    alert(‘get cookie userName: ‘+userName);
<!-- lang: html -->
    result = checkCookie(‘userName‘) ? ‘Yes‘ : ‘No‘;
<!-- lang: html -->
    alert(‘checke cookie userName is exist?‘+result);
<!-- lang: html -->

<!-- lang: html -->
        /**
<!-- lang: html -->
        *设置Cookie
<!-- lang: html -->
        *@param c_name  string 要设置的cookie的名称
<!-- lang: html -->
        *@param value  mixed 要设置的cookie的值
<!-- lang: html -->
        *@param expiredays  int 设置cookie多少天后过期
<!-- lang: html -->
        */
<!-- lang: html -->
        function setCookie(c_name, value, expiredays){
<!-- lang: html -->
            var exdate = new Date();
<!-- lang: html -->
            exdate.setDate(exdate.getDate() + expiredays);
<!-- lang: html -->
            document.cookie = c_name+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString());
<!-- lang: html -->
        }
<!-- lang: html -->

<!-- lang: html -->
        /**
<!-- lang: html -->
        *根据Cookie名获取Cookie值
<!-- lang: html -->
        *@param c_name  string Cookie名
<!-- lang: html -->
        *@return  string
<!-- lang: html -->
        */
<!-- lang: html -->
        function getCookie(c_name){
<!-- lang: html -->
            if(document.cookie.length >0){
<!-- lang: html -->
                c_start = document.cookie.indexOf(c_name+"=");
<!-- lang: html -->
                if(c_start != -1){
<!-- lang: html -->
                    c_start = c_start + c_name.length + 1;
<!-- lang: html -->
                    c_end = document.cookie.indexOf(";", c_start);
<!-- lang: html -->
                    if(c_end == -1){
<!-- lang: html -->
                        c_end = document.cookie.length;
<!-- lang: html -->
                    }
<!-- lang: html -->
                    return unescape(document.cookie.substring(c_start, c_end));
<!-- lang: html -->
                }
<!-- lang: html -->
            }
<!-- lang: html -->
            return "";
<!-- lang: html -->
        }
<!-- lang: html -->

<!-- lang: html -->
        /**
<!-- lang: html -->
        *删除设置的Cookie
<!-- lang: html -->
        *@param c_name  string 设置的cookie的名称
<!-- lang: html -->
        */
<!-- lang: html -->
        function delCookie(c_name){
<!-- lang: html -->
            var exp = new Date();
<!-- lang: html -->
            exp.setTime(exp.getTime() - 1);
<!-- lang: html -->
            var cval = getCookie(c_name);
<!-- lang: html -->
            if(cval != null){
<!-- lang: html -->
                document.cookie = c_name+"="+cval+";expires="+exp.toGMTString();
<!-- lang: html -->
            }
<!-- lang: html -->
        }
<!-- lang: html -->

<!-- lang: html -->
        /**
<!-- lang: html -->
        *检查某Cookie是否存在
<!-- lang: html -->
        *@param c_name  string 设置的Cookie名称
<!-- lang: html -->
        *@return boolean
<!-- lang: html -->
        */
<!-- lang: html -->
        function checkCookie(c_name){
<!-- lang: html -->
            var value = http://www.mamicode.com/getCookie(c_name);>

JavaScript Cookie