Part of an ActionScript 3 project I was working on required me to pull cookies from the users browser into Flash. I tried hunting around google to find a good method to achieve this, but found a lot of articles that wanted you to alter the embed code for flash. I wanted to find a way inside Flash to read the users cookies.
I wrote some code to do something similar in ActionScript 2 that used
LoadVars. This class has been removed in ActionScript 3.
This is the ActionScript 3 code I came up with. It uses
ExternalInterface to execute a bit of javascript that will pull the cookies directly into flash. It alters the cookie string to make it look like a URL and runs it through
URLVariables.
02 | import flash.display.Sprite; |
03 | import flash.external.ExternalInterface; |
04 | import flash.net.URLVariables; |
06 | public class BrowserCookies extends Sprite |
09 | private var _urlVariables:URLVariables; |
16 | public function get urlVariables() : URLVariables { |
26 | public function getCookieValue(value: String ) : String { |
27 | var returnValue: String = "" ; |
29 | if (_urlVariables && _urlVariables[value]) { |
30 | returnValue = _urlVariables[value]; |
39 | public function BrowserCookies() : void { |
41 | var browserCookieString: String ; |
44 | browserCookieString = ExternalInterface.call( "function(){return document.cookie}" ); |
47 | browserCookieString = browserCookieString.replace(/;\s/g, "&" ); |
50 | if (browserCookieString) { |
51 | _urlVariables = new URLVariables(browserCookieString); |
Now you can access any cookie property inside flash. Here is a quick example of how to use the class. Assume BrowserCookies.as exists in the same folder as the example class.
03 | import flash.display.Sprite; |
04 | import flash.text.TextField; |
06 | public class Index extends Sprite{ |
07 | public function Index(){ |
09 | var urlVars:BrowserCookies = new BrowserCookies; |
10 | var textField:TextField = new TextField; |
12 | textField.width = 200 ; |
13 | textField.height = 200 ; |
14 | textField.text = urlVars.getCookieValue( "today" ); |
I've uploaded a Flex Projext Export version of this example available
here. Feel free to contact me if you have any questions.
Does this work on all browsers?
ReplyDeleteThere shouldn't be a browser issue. I tested the script in Chrome, Firefox and IE. I didn't test it in Opera, but if you access the cookies properties in the same way it should work fine.
ReplyDeleteawesome. thanks.
ReplyDeleteVery valuable, thanks !!!
ReplyDeleteHi, after a lot of Google searches this appears to be exactly what I'm after for a project, however I am unable to get it to work. I get this error:
ReplyDeleteReferenceError: Error #1065: Variable addFrameScript is not defined.
at BrowserCookies()
Does this only work if you compile it using Flex or is there a method for compiling it via Flash CS4. I am quite the AS3 novice so any help/pointers will be greatly appreciated.
I'm not using the method addFrameScript in the BrowserCookies class so that shouldn't be an issue. You may want to double check your code to see if it's related to something else.
ReplyDeleteThat's a great example. Thanks a lot! I think it can be done a bit shorter and simpler, like this:
ReplyDeletepackage com.flashdroid.utils
{
import flash.external.ExternalInterface;
import flash.net.URLVariables;
public var Cookies: URLVariables = new URLVariables(ExternalInterface.call("function(){return '8=); '+document.cookie}").replace(/;\s/g, "&"));
}
Essentially it does the same, but you can import the code and access cookies using static Cookie URLVariables object, i.e. Cookies['name']
Works for me :)
Hi there,
ReplyDeleteYour code is useful and should work in many cases.
There will be a problem however, if any of the cookie values contain an "&" sign. (FaceBook does this in their FaceBook connect implementation, for example.) It will incorrectly separate the cookie value.
Best regards.
Thanks for sharing such valuable information. Keep posting such great info for us, thanks.
ReplyDeleteHi,why doesn't Internet Explorer allow to access document object in ActionScript. for example this code works well in all browsers but doesn't works in Internet Explorer, do you know why and know how to solve my problem? I wan't to access the cookie.
ReplyDeleteExternalInterface.call("eval","alert(document.cookie)");
It should work ok in ie. It looks like you're trying to eval a alert response which won't work. Try using the example code I have and return the values through an anonymous function.
Deletecould u upload again the example file plz
ReplyDeleteThanks for letting me know it was broken.Should be fixed now.
DeletePerfeito ^^ VALEU!
ReplyDelete