Tuesday, June 23, 2009

Setting and Reading Cookies in Flash AS2

View the actionscript 3 version of this post
Setting a cookie with Flash (Actionscript 2.0) is really easy. Reading that cookie in Flash (or passing the value of the cookie back into flash) isn't so easy (well, at least that's what the internet would have you believe). After searching for some documentation on the matter without any luck, I took matters into my own hands. It turns out if you use the jquery library, it is actually pretty easy.

First of all, you need to make sure you are using SWFobject to embed your flash, like this example: (make sure you have the swfobject js file included in your head)
<div id="flashcontent">Alt Content Goes Here</div>
<p><script type="text/javascript"> var so = new SWFObject("HomePageFlash.swf", "mymovie", "573", "434", "9", "#f0af2c");
  so.addParam("quality", "high");
  so.addParam("wmode", "transparent");

Then you need jquery installed.
And you also need the jquery cookie plugin included.

To set a cookie from flash you just need to call a js function that is in the cookie plugin thusly:
getURL("javascript:$.cookie('flash_cookie', 'the_value')");
You can add as many params as you need thisly:
getURL("javascript:$.cookie('flash_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true })");

Now for the exciting part. If you want to read that cookie in flash, all you need to do is pass it in using addVariables. In the SWFobject, just add this line of code BEFORE it writes the flash (so before the so.write part in my example):

  so.addVariable("theCookie", $.cookie('flash_cookie'));

('theCookie' being a variable that will be passed into flash and the $.cookie part will pull the value of that cookie and dump it into 'theCookie')

If you have questions, feel free to comment.

(issues: Setting cookies in flash, reading cookies with flash, passing cookies into flash, as2, actionscript2, flash 8, pass cookies with addVariables, pass cookies with SWFobject)


George said...

Hi Nate. Great article! I'm actually trying to set&read cookies in AS2 using your method. However, when I add "so.addVariable("theCookie", $.cookie('flash_cookie'));", the browser does not display the flash anymore, but the alternate content. When I remove the addVariable line, flash shows again. Any ideas how to get around this? Thanks!

nate said...

I'm not sure why it would be doing that. All that should do is pass the contents of "flash_cookie" into your flash movie as a variable called "theCookie". You need to make sure you don't define the variable "theCookie" in your flash file, because that will mess things up. I don't think that would make the flash not show up, though. If you want to give me your email, I can send you an example that I know works and you can dig around in there.

George said...

Thanks for your quick reply and sorry to get back so late... I've found a way around it, but thanks anyways!

javieth said...
This comment has been removed by a blog administrator.
Author said...

Learn adobe flash,actionscript, as3, html5, flex tutorials

adobe flash,actionscript, as3, html5, flex

nishaa said...

Dear Nate,

Its great to look it. However, If show/send the working example of it, then it will be great for the users to use it.

My email-id is - nisha.interactive@gmail.com

Google android app development said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
Android app development companies| Android phone app development|