The front page All those lovely tutorials Other sites of interest More info about Stickman
Stickman

Cookies

Read, write and delete cookies using Flash and JavaScript in combination


 TUTORIAL INFO 

Version Flash 4
Difficulty Advanced
Created 1st Nov 2000

 OTHER LANGUAGES 

Français

 DOWNLOAD 

ZIP

FLA

I'm working on a project that needs to access cookies in a seamless way. I've seen a few different methods, but couldn't find one that did everything that I needed in an easily-understood (and transparent) way. It took me a while, but I managed to piece this technique together from several sources (and with a bit of my own guesswork).

You mean this isn't your own work?

Well, yes and no. While the final file and the JavaScript are pretty much all my work, I did get a lot of help from several sources. First of all, Colin Moock's very well-written introduction to using FSCommand with JavaScript. Also, this FSCommand JavaScript Library at AshzFall both enlightened and confused me in equal measure. Finally, certain threads on We're Here helped a great deal.

Yeah, yeah. Get on with it

Right, this is an advanced tutorial so I'm not going to explain what cookies are, how they work or why you might need them (for more info on this subject, try CookieCentral). Nor am I going to go into detail regarding FSCommand -- as mentioned above, Colin Moock's site has an excellent tutorial on this subject, so I'd recommend you check it out.

Instead, here's a quick run-down of how to get it working...

1) Insert the script

Included in the .zip file you'll find some example HTML which includes all the script you need to read, write and delete cookies (alternatively, look at the source of this page). Also included is some VBScript that's also required.

Just copy and paste this script into your document, making sure to change all instances of f4cookie to your movie's ID. This parameter is set in the <OBJECT ID=...> tag and also the <EMBED NAME=...> tag. It's just a label for the movie that tells JavaScript where to send data.

You'll also need to insert the following code in the <EMBED...> TAG:

swLiveConnect="true"

This code and the VBScript are what enables the communication between Flash and the browser (ie. JavaScript).

2) Use the FSCommands in Flash

FSCommand works by taking a 'command' and an 'argument'. In this case there are three commands, which work as follows:

So let's say we want to create a cookie called UserName and give it the value Stickman, and have it expire in about a month. We'd use the following syntax:

      FS Command ("set_cookie", "UserName,Stickman,30")

You can use spaces in your values, but not commas (only use commas to separate values, as above).

If we want to retrieve the cookie's value later, we use the following syntax:

      FS Command ("get_cookie", "UserName,result")

When the command has completed, the variable result will hold the value of the cookie UserName (if the cookie is empty or doesn't exist, it will return the value "undefined"). However, there's a small delay between sending the command and the value being returned, so we can't act on its value immediately. Which is why if you look at the code for the GET COOKIE button in the Flash file above, it calls a movie clip called ValueChecker. Having set result to a temporary value ("**waiting**"), this move clip loops, checking until the value of result changes -- indicating that the cookie's value has been returned.

3) That's it

That pretty much sums up how you would use this technique in your own files. As long as the movie ID in your JavaScript is set correctly you shouldn't have any problems.

But how does it work?

Most of the hard work here is being done by the JavaScript, and I'm not about to start giving lessons on that. The JavaScript code is heavily commented, so look through it and things should become clearer.

If you're having problems understanding the whole FSCommand - JavaScript thing then once again, I suggest you check out Colin Moock's tutorial. Also, try fiddling with the values in the Flash file and see what happens.

Good luck!

Stickman

All files and text copyright ©Stickman 1998 - 2003. For copyright and terms of use information, please read this page.