The Source Code of ActionScript 3 Cookie class
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|&uh0j9~[S#S/[`[0Have you ever wanted to store persisten
·
D0The Source Code of Actionscrīpt 3 Cookie class–Enables You to Read, Write, and Edit Shared Objects on the User’s Hard Disk
Have 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:
- import com.communitymx.Cookie;
- var myCookie:Cookie = new Cookie(”yoursite”);
- // user info
- myCookie.username = “nttlib”;
- myCookie.password = “abcdefg”;
- myCookie.save();
- // retrieve cookie info
- trace(myCookie.username); // result: nttlib
- trace(myCookie.password); // result: abcdefg
But…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.
Download:
cookie.as
- package com.nttlib.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 = "nttlib", timeOut:uint=3600) {
- _name = name;
- _time = timeOut;
- _so = SharedObject.getLocal(name, "/" );
- }
- // clear when timeout;
- 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();
- }
- // check timeout
- private function isTimeOut(time:uint):Boolean {
- var today:Date = new Date();
- return time + _time * 1000 < today.getTime();
- }
- // get timeout;
- public function getTimeOut():uint {
- return _time;
- }
- // get cookie name;
- public function getName():String {
- return _name;
- }
- // clear all Cookie value;
- public function clear():void {
- _so.clear();
- }
- // add Cookie item( key-value )
- 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();
- }
- // remove Cookie item by key;
- public function remove(key:String):void {
- if (isExist(key)) {
- delete _so.data.cookie["key_" + key];
- _so.flush();
- }
- }
- // get Cookie item value by key;
- public function get(key:String):Object{
- return isExist(key)?_so.data.cookie["key_"+key]:null;
- }
- // check Cookie item exist;
- public function isExist(key:String):Boolean{
- key = "key_" + key;
- return _so.data.cookie != undefined && _so.data.cookie[key] != undefined;
- }
- }
- }
Cheers!
(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)
更多推荐
已为社区贡献1条内容
所有评论(0)