User Guide
404 Chapter 6: ActionScript Core Classes
Description
Method; returns a reference to a locally persistent shared object that is available only to the
current client.
Note: If the user has selected to never allow local storage for this domain, the object is not saved
locally, even if a value for
localPath is specified. For more information, see “Local disk space
considerations” on page 398.
To avoid name collisions, Flash looks at the location of the SWF file that is creating the shared
object. For example, if a SWF file at www.myCompany.com/apps/stockwatcher.swf creates a
shared object named
portfolio, that shared object will not conflict with another object named
portfolio that was created by a SWF file at www.yourCompany.com/photoshoot.swf because
the SWF files originate from different directories.
Although the
localPath parameter is optional, developers should give some thought to its use,
especially if other SWF files will need to access the shared object. If the data in the shared object
are specific to one SWF file that will not be moved to another location, then use of the default
value makes sense. If other SWF files need access to the shared object or if the SWF file that
creates the shared object will later be moved, then the value of this parameter can have a profound
effect on whether any SWF files will be able to access the shared object. For example, if you create
a shared object with
localPath set to the default value of the full path to the SWF file, then no
other SWF file will be able to access that shared object. If you later move the original SWF file to
another location, then not even that SWF file will be able to access the data already stored in the
shared object.
You can reduce the likelihood that you will inadvertently restrict access to a shared object by using
the
localpath parameter. The most permissive option is to set the localPath parameter to "/",
which makes the shared object available to all SWF files in the domain, but will increase the
likelihood of name collisions with other shared objects in the domain. More restrictive options are
available to the extent that you can append the
localPath parameter with folder names that are
contained in the full path to the SWF file. For example, your
localPath parameter options for
the
portfolio shared object created by the SWF file at www.myCompany.com/apps/
stockwatcher.swf are: "/"; "/apps"; or "/apps/stockwatcher.swf". You will need to determine which
option provides enough flexibility for your application.
Example
The following example creates a shared object that stores text typed into a TextInput component
instance. The resulting SWF file will load the saved text from the shared object when it starts
playing. Every time the user presses Enter, the text in the text field is written to the shared
object.
// create the shared object and set localpath to server root
var my_so:SharedObject = SharedObject.getLocal("savedText", "/");
// load saved text from shared object into myText_ti TextInput component
myText_ti.text = my_so.data.myTextSaved;
// assign an empty string to myText_ti if the shared object is undefined
// to prevent the text input box from displaying "undefined" when
// this script is first run.
if (myText_ti.text == undefined) {
myText_ti.text = "";
}
// create listener object and function for <enter> event
var textListener:Object = new Object();