[AS2]Flash版本模拟Cookie功能的Cookie类
[AS2]Flash版本模拟Cookie功能的Cookie类,利用Flash的SharedObject类的功能,来模拟Cookie的功能!
Cookie类:
- /**
- * @link http://www.klstudio.com
- * @author Kinglong
- * @usage flash player 7
- * @version 0.1
- */
- class as2.util.Cookie {
- private var _time:Number;
- private var _name:String;
- private var _so:SharedObject;
- public function Cookie(name:String,time:Number){
- _name = (name == undefined)?"klstudio":name;
- _time = (time == undefined)?36000:time;
- _so = SharedObject.getLocal(_name,"/");
- }
- public function clearTimeOut():Void{
- var obj:Object = _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:Number):Boolean{
- var today:Date = new Date();
- return time + _time * 1000 < today.getTime();
- }
- public function getTime():Number{
- return _time;
- }
- public function getName():String{
- return _name;
- }
- public function clear():Void{
- _so.clear();
- }
- public function put(key:String,value:Object):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();
- }
- public function remove(key:String):Void {
- if (contains(key)) {
- delete _so.data.cookie["key_" + key];
- _so.flush();
- }
- }
- public function get(key:String):Object{
- return contains(key)?_so.data.cookie["key_"+key]:null;
- }
- public function contains(key:String):Boolean{
- key = "key_" + key;
- return _so.data.cookie != undefined && _so.data.cookie[key] != undefined;
- }
- }
调用代码 :
- import as2.util.Cookie;
- //创建cookie对象,超时单位为毫秒;
- var cookie:Cookie = new Cookie("myCookie", 24 * 60 * 60 * 1000);
- //添加key值;
- cookie.put("a", {a:"a0001"});
- cookie.put("b", {b:2});
- //查key是否存在;
- trace(cookie.contains("a"));
- //删除key;
- cookie.remove("a");
- trace(cookie.contains("a"));
- //获取key的值
- trace(cookie.get("b").b);
- //清除cookie所有的值;
- cookie.clear();
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...