ck168

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1、设置Cookie

//设置cookie
        function setCookie(cookieName, cookieValue, cookieExpires, cookiePath) {
            cookieValue = escape(cookieValue);//编码latin-1
            if (cookieExpires == "") {
                var nowDate = new Date();
                nowDate.setMonth(nowDate.getMonth() + 6);
                cookieExpires = nowDate.toGMTString();
            }
            if (cookiePath != "") {
                cookiePath = ";Path=" + cookiePath;
            }
            document.cookie = cookieName + "=" + cookieValue + ";expires=" + cookieExpires + cookiePath;
        }

2、读取Cookie

//获取cookie
        function getCookieValue(cookieName) {
            var cookieValue = document.cookie;
            var cookieStartAt = cookieValue.indexOf("" + cookieName + "=");
            if (cookieStartAt == -1) {
                cookieStartAt = cookieValue.indexOf(cookieName + "=");
            }
            if (cookieStartAt == -1) {
                cookieValue = null;
            }
            else {
                cookieStartAt = cookieValue.indexOf("=", cookieStartAt) + 1;
                cookieEndAt = cookieValue.indexOf(";", cookieStartAt);
                if (cookieEndAt == -1) {
                    cookieEndAt = cookieValue.length;
                }
                cookieValue = unescape(cookieValue.substring(cookieStartAt, cookieEndAt));//解码latin-1
            }
            return cookieValue;
        }

3、调用,使用方法

        function set()
        {
            setCookie("userName", "ck", "", "");
        }
        function read()
        {
            var value = getCookieValue("userName");
            alert(value);
        }

 

posted on 2016-05-17 14:14  HelloWorld168  阅读(179)  评论(0编辑  收藏  举报