View comments | RSS feed

SharedObject class

Availability

Flash Player 6.

Description

Shared objects are powerful: They offer real-time data sharing between objects that are persistent on the user's computer. You can consider local shared objects as cookies.

You can use local shared objects to maintain local persistence. For example, you can call SharedObject.getLocal() to create a shared object, such as a calculator with memory, in the player. Because the shared object is locally persistent, Flash saves its data attributes on the user's computer when the SWF file ends. The next time the SWF file runs, the calculator will display the values it had when the SWF file ended. Alternatively, if you set the shared object's properties to null before the SWF file ends, the calculator opens without any prior values the next time the SWF file runs.

To create a local shared object, use the following syntax:

// Create a local shared object 
var so:SharedObject = SharedObject.getLocal("foo");

Local disk space considerations

Local shared objects are always persistent on the client, depending on available memory and disk space.

By default, Flash can save locally persistent remote shared objects as large as 100K. When you try to save a larger object, Flash Player shows the Local Storage dialog box, which lets the user allow or deny local storage for the domain that is requesting access. (Ensure that your Stage size is at least 215 x 138 pixels; this is the minimum size Flash requires to display the dialog box.)

If the user clicks Allow, the object is saved and SharedObject.onStatus is invoked with a code property of SharedObject.Flush.Success; if the user clicks Deny, the object is not saved and SharedObject.onStatus is invoked with a code property of SharedObject.Flush.Failed.

The user can also specify permanent local storage settings for a particular domain by right-clicking (Windows) or Control-clicking (Macintosh) while a SWF file is running, selecting Settings, and then opening the Local Storage panel.

You can't use ActionScript to specify local storage settings for a user, but you can display the Local Storage panel for the user by using System.showSettings(1).

The following list summarizes how the user's disk space choices interact with shared objects:

Additionally, if the user selects a value that is less than the amount of disk space currently being used for locally persistent data, the player warns the user that any locally saved shared objects will be deleted.

Note: There is no size limit in Flash Player that runs from the authoring environment.

Method summary for the SharedObject class

Method Description
SharedObject.clear()

Purges all the data from the shared object and deletes the shared object from the disk.

SharedObject.flush()

Immediately writes a locally persistent shared object to a local file.

SharedObject.getLocal()

Returns a reference to a locally persistent shared object that is available only to the current client.

SharedObject.getSize()

Gets the current size of the shared object, in bytes.

Property summary for the SharedObject class

Property Description
SharedObject.data

The collection of attributes assigned to the data property of the object; these attributes can be shared and/or stored.

Event handler summary for the SharedObject class

Event handler Description
SharedObject.onStatus

Invoked every time an error, warning, or informational note is posted for a shared object.

Constructor for the SharedObject class

For information on creating local shared objects, see SharedObject.getLocal().


Comments


Zerg said on Feb 26, 2005 at 4:11 AM :
// why thisc code return an error , what to do ?

var my_local:SharedObject = SharedObject.getLocal("MYLOCAL");
if (my_local.data.master == undefined) {
my_local.data.master = "Mamyan";
} else {
trace("Your Shared Object [Master] is ["+my_local.data.master+"]");
}
if (my_local.data.cash == undefined) {
my_local.data.cash = 1500;
} else {
trace("Your Shared Object [Cash] is ["+my_local.data.cash+"]");
}
my_local.flush(1000);
my_local.clear()

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 13: There is no method with the name 'clear'.
my_local.clear()

Total ActionScript Errors: 1 Reported Errors: 1
dericos said on Jul 26, 2005 at 12:39 PM :
Does anyone get this stuff to work? Their own books and documentation contradict each other.
michieldb said on Aug 26, 2005 at 3:28 AM :
Can I Connect an cookie from another swf-object then the one I createt it?
plz help me !!
michiel_de_backker@hotmail.com
Kevin_H said on Aug 31, 2005 at 9:22 AM :
In the Local disk space considerations section I read the following:
By default, Flash can save locally persistent remote shared objects as large as 100K.

If the user has their local storage setting at 100K, is that 100K per object\domain or all objects\domains?
kev - signals.co.uk said on Sep 7, 2005 at 9:20 AM :
use the following instead of the clear() method:

for (var i in preferences_so.data) { delete preferences_so.data[i];}
delete my_local;
kev - signals.co.uk said on Sep 7, 2005 at 9:21 AM :
whooops... i meant:

for (var i in my_local.data) { delete my_local.data[i];}
delete my_local;
No screen name said on Feb 20, 2006 at 7:44 AM :
my.local.clear();

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001664.html