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?
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?