PDA

View Full Version : Setting and retrieving cookies from Flash using Javascript


wizdave
02-08-2006, 10:07 PM
My basic need is the ability to show a flash video only once per browser session without using Server-side technologies to track the session.

I originally tried using Shared Objects and they work well, but I could not figure out how to make them expire when the browser session ended. I could set a date variable in the Shared Objects and play the video again if the current date was more than an hour (or 30 minutes or whatever) past the previous play time, but I would rather have the shared object "expire" when the browser session ended.

Cookies could provide this functionality, but I have been having trouble retrieving Javascript based cookies in Flash (using getURL and document.cookie). If you do not set an expiration on Javascript cookies, they will expire when the browser session ends.

Here's the code I have been trying to get working for Javascript cookies in Flash:


function GetCookie(cookieName) {
js = "javascript:function gc(){";
js += "var s = '" + cookieName + "=';";
js += "var r = 'false';";
js += "var c = document.cookie;";
js += "if (c.length > 0) {";
js += "o = c.indexOf(s);";
js += "if (o != -1) {";
js += "o += s.length;";
js += "e = c.indexOf(';', o);";
js += "if (e == -1) e = c.length;";
js += "r = unescape(c.substring(o, e));";
js += "}";
js += "}";
js += "var f = document.getElementById('flashLoader');";
js += "if(f) {";
js += "f.SetVariable('cookieVariable', r);";
js += "}";
js += "}gc();";
getURL(js);
return cookieVariable;
}

function SetCookie(cookieName, cookieValue) {
js = "javascript:function sc(){";
js += "var c = escape('" + cookieName + "') + '=' + escape('" + cookieValue + "') + '; path=/';";
js += "document.cookie = c;";
js += "}sc();";
getURL(js);
}


The SetCookie function works, but the GetCookie function is always returning an empty sting. Can anyone help me fix these functions I have written?

fugitive123
02-09-2006, 12:53 AM
Brilliant idea. But I must say you can't simply return the value from the javascript without using "asfunction" to pass it back to the Flash function. You need a "onEnterFrame" or "setInterval" to check whether a value has been passed back to Flash and then terminate the ongoing loop.

Cota
02-09-2006, 01:20 AM
Read this thread
http://www.actionscript.org/forums/showthread.php3?t=63951

wizdave
02-10-2006, 03:48 PM
I took a look at that thread about JavaScript cookies and I tested it out. If I put a text field with cookieVariable as the Var property, it correctly gets set. My GetCookie function above finishes executing before cookieVariable is set and therefore is returning an empty string every time. What would be the best way to either delay the return on the function or retrieve the variable after it has been set and resume execution of the remainder of the code after I have the value of the cookie?

Until I can figure this out, I am probably gonna go with setting an expiration date variable in my shared object and compare that to the current date to determine whether or not I should play the video.

wizdave
02-10-2006, 05:46 PM
Here's an working GetCookie function thanks to Flash 8 External API (and MichealxxOA in this thread (http://actionscript.org/forums/showthread.php3?t=96417)).


import flash.external.ExternalInterface;

function GetCookie(cookieName) {
var r = "";
var search = cookieName + "=";
var js = "function get_cookie(){return document.cookie;}"
var cookieVariable = ExternalInterface.call(js).toString();

if(cookieVariable.length > 0) {
offset = cookieVariable.indexOf(search);
if (offset != -1) {
offset += search.length;
end = cookieVariable.indexOf(";", offset);
if (end == -1) end = cookieVariable.length;
r = unescape(cookieVariable.substring(offset, end));
}
}
return r;
}

BluegillMedia
01-21-2008, 03:21 PM
I am trying to use WizDave's method, but it isn't quite working right. Can someone tell me what I am doing wrong? Do I have to set FSCommand to equal true or anything like that in flash? (Javascript is in next post).

My ActionScript looks like this:

import flash.external.ExternalInterface;
function SetCookie(cookieName, cookieValue) {
js = "javascript:function sc(){";
js += "var c = escape('" + cookieName + "') + '=' + escape('" + cookieValue + "') + '; path=/';";
js += "document.cookie = c;";
js += "}sc();";
getURL(js);
}

function GetCookie(cookieName) {
var r = "";
var search = cookieName + "=";
var js = "function get_cookie(){return document.cookie;}";
var cookieVariable = ExternalInterface.call(js).toString();
if (cookieVariable.length > 0) {
offset = cookieVariable.indexOf(search);
if (offset != -1) {
offset += search.length;
end = cookieVariable.indexOf(";", offset);
if (end == -1) {
end = cookieVariable.length;
}
r = unescape(cookieVariable.substring(offset, end));
}
}
return r;
}

_root.resulta.text = "COOKIE=" + GetCookie("intro");

BluegillMedia
01-21-2008, 05:57 PM
Here is the javascript I am using, but the alerts are not running:

function sc(name, value)
{
document.cookie = name+"="+value;
alert(document.cookie);
}


function get_cookie( id, defaultValue ) {
var re = new RegExp(id+'=(.*)');
var value = re.exec(document.cookie);
return (value) ? value[1].split(';')[0] : defaultValue;
alert(document.cookie);
}

Is there an easier way to do this? I have a movie in my flash movie that I want to not play the second time they come. So half way through my flash movie i thought i would set a cookie (after watching the movie) and then I would check for that before playing the movie. Any thoughts?

Please help....

socticus
11-21-2010, 09:56 PM
did you ever figure out what wasn't working between between your flash and javascript?
i am trying to set a cookie with flash, then read that cookie with javascript.
i am having a tough time getting this to work myself. an update on your progress would be really helpful. thanks!

Cota
11-22-2010, 03:22 PM
Using localConnection should make things easier. Assuming you're using AS3.

socticus
11-22-2010, 03:32 PM
i have a flash file that sets the cookie.
i have a separate html file that i want to use javascript to read the cookie, then use that cookie as a value to load a new swf.
i am using AS2. (i am not familiar yet with AS3)

the cookie is set. i can see that i have created to cookie correctly.
i just need help with how to do the javascript. have done a ton of searching on this.

thanks.

BluegillMedia
11-22-2010, 05:25 PM
Sorry, that post was so old that I can't even remember what project it was for. My new suggestion would be to check the cookie with javascript and then pass it via a variable to the flash movie rather then trying to talk back and forth. Best of luck, sorry I am not more help.