SharedObjects in actionscript 3



Shared objects or flash cookies as you can  call them are the official way to store data at client side.  Shared objects are like html cookies, but better in terms of capacity and persistence. Information stores in shared objects (.so) files does not get cleared when you clear browser cache / history data. shared objects can be local / remote. Shared objects can also store bitmap data at client end. Here we will see only a simple local version of it.

* Note: Do not store sensitive information on client side.

var items:Array = new Array(1, 2,3);
var username:String = admin;
var password:String = "xyz123";
var my_so:SharedObject = SharedObject.getLocal("cookie");
my_so.data.itemNumbers = items;
my_so.data.userName = username;
my_so.data.password = password;
my_so.flush();

This is how you store data in a shared object. The “data” property of a shared object is like a object. It can store other data types as its properties, like  String , Number, Boolean, Array, XML etc: shared object can also be used to serialize complex data types as well. Below we see a example using shared objects to store some values.

import flash.events.MouseEvent;
var so:SharedObject = SharedObject.getLocal("flashvisions_survey");
if(so.data.details != undefined) load();
btnSave.addEventListener(MouseEvent.CLICK, save);

function load()
{
if(so.data.details != undefined)
{
var array:Array = so.data.details;

txtfname.text = array[0];
txtlname.text = array[1];
txtlikeflash.text = array[2];
txtsharedobject.text = array[3];
}
}

function save(me:MouseEvent)
{
var array:Array = new Array();
array.push(txtfname.text);
array.push(txtlname.text);
array.push(txtlikeflash.text);
array.push(txtsharedobject.text);

so.data.details = array;
so.flush();
}

Get Adobe Flash player

Download  Source: Here

Popularity: 10% [?]




October 10, 2009   Posted in: Actionscript 3.0, General

Leave a Reply