Notebook Margins Playhouse

Go to the home page of Notebook Margins Playhouse Go to the "Me" section Go to the "Thoughts" section Go to the "Little Lab" section Go to the "Art" section Flash Things

Flash4 Cookie Tutorial and HTMLTemplate

First be aware.
This method uses Javascript, which doesn't communicate with Flash within IE on the Mac. If you have an important business need for using a cookie you would be better off with a server side tool, which doesn't suffer from this limitation. But for artists and experimenters, this method is pretty nice. The best use of it may be to save a string on the hard drive if you want to distribute via CD. A cookie only saves one string. If you want to save more than one string, you might want to use something like a stack or a list and concatenate the string values together before saving them. On reentry pop the values back off the stack or list to get back your information.

Also, be sure to test with cookies disabled to make sure that your movie degrades gracefully if cookies are turned off.

So how do you use the cookieJar HTML template?
First place the cookieJar.html file in your Flash HTML template directory. On windows this default would be C:/Program Files/Macromedia/Flash/HTML/.

Before you publish your fla, go to File/Publish on the menu. Choose the HTML tab and change the Template to CookieJar.

When you want to update the value of the cookie send an FS Command with "setCookies" as the command and the string you want to save as the argument.

For example this code could be put in a button.

On (Release)
FS Command ("setCookies", "Peanut Butter")
End On

Clicking this button would then set the value of the cookie to the string "Peanut Butter". The Javascript code will then also update the "/:CookieJar" variable in Flash to "Peanut Butter" as well. The code tells the cookie to persist for a year from the last time it was set.

I would suggest that you don't use the "/:CookieJar" variable directly as the second argument in the call to FS Command. If you want to use a variable, create it separately, calling it "/:CookieValue", for example. That would make it easier to debug and also would avoid situations where Javascript and ActionScript both are trying to change the CookieJar variable at the same time.

So you would write to CookieValue and send that to the JavaScript code. The JavaScript code writes to CookieJar which you only read from.

There is an example .fla that you can use to examine this more. You can look at it here.

So how does it work?
When you first come to the page there is an OnLoad handler in the HTML that is called when the page first finishes loading. This handler calls the FSCommand Function with "GetCookies" as the command. This function gets the cookie value if it it exists, otherwise it initializes the cookie with the string value "initialized" and saves it for the user.

Now the Flash movie can call the FSCommand function further with "setCookies" and "a string value" as described above, which will update the cookie value to what was set.

cookieJar.html template - this is a blank HTML template so use "view source"
cookieJar.fla
- the example .fla