js Cookie的操作

js操作Cookie
js Cookie的操作
 
 
function StringTrim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); }
    //
    function Session(ckName, ckValue, ckDate) {
        if (!ckName && !ckValue && !ckDate) {
            return false;
        }
        //写入cookies
        else if (ckName && ckValue) {
            //设置过期时间
            var day = new Date();
            var expDay = new Date();
            if (!ckDate || ckDate == 'undefined')
                ckDate = 0;
            expDay.setTime = day.getTime() * 3600000 * 24 * ckDate;
            try{
            document.cookie = escape(StringTrim(ckName)) + '=' + escape(ckValue) + ';expiress' + expDay.toGMTString();
            }
            catch(ee){ return false;}
            return true;
        }
        //读取Cookie
        else if (ckName && !ckValue) {
            ckName = escape(ckName);
            var cookieList = document.cookie.split(';');
            for (var j = 0; j < cookieList.length; j++) {
                var s = StringTrim(cookieList[j].split('=')[0]);
                if (StringTrim(ckName) == StringTrim(cookieList[j].split('=')[0])) {
                    return (cookieList[j].split('=')[1]);
                }
            }
            return '';
        }
    }

 

   用的时候跟用session差不多
 存值  Session("key","伤心木乃伊JS操作Cookes");
 取值  var Value=Session("key");
js Cookis的操作
posted @ 2011-10-10 21:58  伤心木乃伊  阅读(317)  评论(0编辑  收藏  举报