Cookie类

[AS3]Cookie类

[AS2]Flash版本的Cookie类
view plaincopy to clipboardprint?
/**    
 * @link http://www.klstudio.com    
 * @author Kinglong  
 * @playerversion flash player 9   
 * @asversion 3.0  
 * @version 0.5  
 */     
  
package com.klstudio.util {   
       
    import flash.net.SharedObject;     
  
    public class Cookie {   
           
        private var _time:uint;   
        private var _name:String;   
        private var _so:SharedObject;   
           
        public function Cookie(name:String = "klstudio", timeOut:uint=3600) {   
            _name = name;   
            _time = timeOut;   
            _so = SharedObject.getLocal(name, "/");   
        }   
           
        //清楚超时内容;   
        public function clearTimeOut():void {   
            var obj:* = _so.data.cookie;   
            if(obj == undefined){   
                return;   
            }   
            for(var key in obj){   
                if(obj[key] == undefined || obj[key].time == undefined || isTimeOut(obj[key].time)){   
                    delete obj[key];   
                }   
            }   
            _so.data.cookie = obj;   
            _so.flush();   
        }   
           
        private function isTimeOut(time:uint):Boolean {   
            var today:Date = new Date();           
            return time + _time * 1000 < today.getTime();   
        }   
           
        //获取超时值;   
        public function getTimeOut():uint {   
            return _time;   
        }   
           
        //获取名称;   
        public function getName():String {   
            return _name;   
        }   
           
        //清除Cookie所有值;   
        public function clear():void {   
            _so.clear();   
        }   
           
        //添加Cookie值   
        public function put(key:String, value:*):void {   
            var today:Date = new Date();   
            key = "key_"+key;   
            value.time = today.getTime();   
            if(_so.data.cookie == undefined){   
                var obj:Object = {};   
                obj[key] = value;   
                _so.data.cookie = obj;   
            }else{   
                _so.data.cookie[key] = value;   
            }   
            _so.flush();   
        }   
           
           
        //删除Cookie值;   
        public function remove(key:String):void {   
            if (contains(key)) {   
                delete _so.data.cookie["key_" + key];   
                _so.flush();   
            }   
        }   
           
        //获取Cookie值;   
        public function get(key:String):Object{        
            return contains(key)?_so.data.cookie["key_"+key]:null;   
        }   
           
        //Cookie值是否存在;   
        public function contains(key:String):Boolean{   
            key = "key_" + key;    
            return _so.data.cookie != undefined && _so.data.cookie[key] != undefined;   
        }   
    }   
}
文章来自: 闪无忧(www.5uflash.com) 详文参考:http://www.5uflash.com/flashjiaocheng/Flash-as3-jiaocheng/2945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值