[AS2]Flash版本模拟Cookie功能的Cookie类
2012-02-06 12:14:04 来源:本站整理核心提示:Cookie类:/** * @link http://www.klstudio.com * @author Kinglong * @usage flash player 7 * @version 0.1 */ classas2.util.Cookie { private var _time:Numb
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();
- ››asp.net页面弄成伪静态页面
- ››Asp.net 中将汉字转换成拼音的方法
- ››ASP.NET及JS中的cookie基本用法
- ››ASP.NET获取MS SQL Server安装实例
- ››asp.net实现调用百度pai 在线翻译英文转中文
- ››ASP.NET页面选项进行提示判断
- ››Asp.net定时执行程序
- ››ASP.NET中利用DataList实现图片无缝滚动
- ››ASP.NET验证控件RequiredFieldValidator
- ››ASP.NET中使用System.Net.Mail发邮件
- ››ASP.NET中获取用户控件中控件的ID
- ››ASP.NET中FileBytes写成文件并存档
更多精彩
赞助商链接