You will notice that I am not referring directly to the variable mySaveText using myCookie.mySavedText but that instead I am using myCookie.data.mySavedText.
From the Flash 8 manual :
data (SharedObject.data property)
The collection of attributes assigned to the data property of the object; these attributes can be shared and/or stored. Each attribute can be an object of any basic ActionScript or JavaScript type--Array, Number, Boolean, and so on. For example, the following lines assign values to various aspects of a shared object:
In simple words, each time you want to assign or retrieve a piece of data from a shared object you need to be aware that it is always stored as an attribute of the data property of the shared object.Each attribute can be an object of any basic ActionScript, that is a neat feature too as we can store arrays as an attribute of the data property.
Let's go back to our code, if we detect that myCookie.data.mySavedText is undefined we don't want to write anything in the text field (if we don't do that test it will display undefined in the text field.)
Now we want to write a bit of code that will detect when some text has been entered in the input text field and
write that text in the shared object as it is being typed. here we go :
var myCookie = SharedObject.getLocal("mySavedData");
Let's examine that new bit of code. To detect if some new characters are entered in the text field we use the event method onChanged. That method will be triggered each time something new is typed.
If something new is typed we save the entire content of the text field in the mySavedText attribute of the data property of our shared object.
We use myCookie.flush() to write the data in the shared object immediately, if we don't do that the data will be written to the shared object when you close your SWF or your browser (this is what happened by default) . Since a computer might not wait that you close your browser to crash we need to write the data as soon as it is entered :)
The last thing we want to do is to delete the shared object when the submit button is pressed, this is done very easily by adding the following bit of code :
var myCookie = SharedObject.getLocal("mySavedData");
The clear method of the SharedObject class purges all the data from the shared object and deletes the shared object from the disk.
We are now done, we can now safely type a long creative text without fearing that we are going to loose it all if the computer crashes. Test it by yourself below, type anything in the box below then you can close your browser or even clean your cache but when you come back to the page your text will still be here, just click on the submit button to delete the shared object from your disk.