JavaScript的Cookie类

 /*

JavaScript的Cookie类

注:js设定cookie的时间是以毫秒为单位

*/

function jsCookie(){

    var COOKIE_LIFE ={year:31536000,month:2592000,week:604800,

                      day:86400,hour:3600,browser:0}

    

    this.expires = 'browser';

    this.path = null;

    this.domain = null;

    this.secure = null;

    

    this.get=function(fieldName){    

        var regexp = window.eval("/"+fieldName+"=([//w%,]+)(|;)/");

        var result = document.cookie.match(regexp);

        return (result?unescape(result[1]):null);

    }    

    this.set=function(fieldName,fieldValue){

        var cookie_list;

        cookie_list = fieldName+"="+escape(fieldValue);        

        cookie_list += expires_param(this.expires);

        cookie_list += path_param(this.path);

        cookie_list += domain_param(this.domain);

        cookie_list += secure_param(this.secure);

        document.cookie = cookie_list;

    }

    function expires_param(expires){

        var tm=0;

        if(typeof(expires)=='string'){

            if(!COOKIE_LIFE[expires]) return "";    

            tm = COOKIE_LIFE[expires]*1000;        

        }else if(typeof(expires)=='number'){            

            tm = expires*1000;

        }else{return "";}

        expires = ";expires="+new Date(new Date().getTime()+ tm).toUTCString();

        return expires;

    }

    function path_param(path){

        if(!path) return "";

        return (";path="+path);

    }

    function domain_param(domain){

        if(!domain) return "";

        return (";domain="+domain);

    }

    function secure_param(secure){

        if(secure!='secure') return "";    

        return ";secure";

    }

}



示例:



<script language="javascript">

ck = new jsCookie();

ck.expires = 'browser';

//ck.path='/';

//ck.domain='.xxx.com';

//ck.secure = 'secure';

ck.set("abc","123456");

alert(ck.get("abc"));

</script>







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值