The Source Code of ActionScript 3 Cookie class

来源:互联网 发布:疯狂java培训怎么样 编辑:程序博客网 时间:2024/04/28 04:06

D0The Source Code of Actionscrīpt 3 Cookie class–Enables You to Read, Write, and Edit Shared Objects on the User’s Hard Disku*Su#M }j|&uh0

j9~[S#S/[`'[0Have you ever wanted to store persistent information in your Flash movies, save login information, shopping cart data, user preferences, even complex objects like arrays? If you have purchased “Flash Extension”, simply import the cookie class and copy the source code like following:闪吧新社区1u4n6k*k/aM

  1. import com.communitymx.Cookie;
  2. var myCookie:Cookie = new Cookie(yoursite);
  3. // user info
  4. myCookie.username = “nttlib”;
  5. myCookie.password = “abcdefg”;
  6. myCookie.save();
  7. // retrieve cookie info
  8. trace(myCookie.username); // result: nttlib
  9. trace(myCookie.password); // result: abcdefg

*SCG:b.B4V9hc _#l0But…Maybe you have noticed the word “purchased”–yes, it is not free! The following is a free, similar above cookie class, using it you can set cookies in flash to remember a user’s music preference now. :)

A9O9s)uC0

闪吧新社区4[F C Q$Ie

闪吧新社区;FC8N9u^8[%[R

闪吧新社区U A?0XMEHx

Download: cookie.as
  1. package com.nttlib.util { 
  2.     import flash.net.SharedObject;  
  3.     public class Cookie { 
  4.         private var _time:uint;
  5.         private var _name:String;
  6.         private var _so:SharedObject;
  7.         public function Cookie(name:String = "nttlib", timeOut:uint=3600) { 
  8.             _name = name;
  9.             _time = timeOut;
  10.             _so = SharedObject.getLocal(name, "/" );
  11.         }
  12.         // clear when timeout;
  13.         public function clearTimeOut():void { 
  14.             var obj:* = _so.data.cookie;
  15.             if(obj == undefined){ 
  16.                 return;
  17.             } 
  18.             for(var key in obj){ 
  19.                 if(obj[key] == undefined || obj[key].time == undefined || isTimeOut(obj[key].time)){ 
  20.                     delete obj[key];
  21.                 } 
  22.             } 
  23.             _so.data.cookie = obj;
  24.             _so.flush();
  25.         } 
  26.         // check timeout
  27.         private function isTimeOut(time:uint):Boolean { 
  28.             var today:Date = new Date();        
  29.             return time + _time * 1000 < today.getTime();
  30.         } 
  31.         // get timeout;
  32.         public function getTimeOut():uint { 
  33.             return _time;
  34.         } 
  35.         // get cookie name;
  36.         public function getName():String { 
  37.             return _name;
  38.         } 
  39.         // clear all Cookie value;
  40.         public function clear():void { 
  41.             _so.clear();
  42.         } 
  43.         // add Cookie item( key-value )
  44.         public function put(key:String, value:*):void { 
  45.             var today:Date = new Date();
  46.             key = "key_"+key;
  47.             value.time = today.getTime();
  48.             if(_so.data.cookie == undefined){ 
  49.                 var obj:Object = {};
  50.                 obj[key] = value;
  51.                 _so.data.cookie = obj;
  52.             }else{ 
  53.                 _so.data.cookie[key] = value;
  54.             } 
  55.             _so.flush();
  56.         } 
  57.         // remove Cookie item by key;
  58.         public function remove(key:String):void { 
  59.             if (isExist(key)) { 
  60.                 delete _so.data.cookie["key_" + key];
  61.                 _so.flush();
  62.             } 
  63.         } 
  64.         // get Cookie item value by key;
  65.         public function get(key:String):Object{     
  66.             return isExist(key)?_so.data.cookie["key_"+key]:null;
  67.         } 
  68.         // check Cookie item exist;
  69.         public function isExist(key:String):Boolean{ 
  70.             key = "key_" + key
  71.             return _so.data.cookie != undefined && _so.data.cookie[key] != undefined;
  72.         } 
  73.     } 
  74. }

闪吧新社区"^ZT_d"Us2I

Cheers!

2z"]l.vBE0 闪吧新社区J3LMr le~L0Q?

(from:http://ntt.cc/2008/07/13/the-source-code-of-actionscrīpt-3-cookie-class-enables-you-to-read-write-and-edit-shared-objects-on-the-user-hard-disk.html)

_;ts5wSL_?N0

原创粉丝点击