As promised I am giving you guys my CookieJar class… this is probably one of the simplest yet most useful utility classes I have written. It’s extremely easy to use… I’ve included inline with my post the class source and an example use as you would see it in an fla. I’ve also included a zip file of all the working source. Any feedback would be wonderful. Take Care guys.
_Michael
Code in the class:
import flash.external.ExternalInterface;
/*
* This class was created to manage Cookies from directly within flash
* Here we will eliminate the need to have an environment outside of flash
* maintain our cookies.
*
*@author: Michael Avila
*@version: 1.0
*/
class CookieJar
{
private static var write_cookie:String = "function writeCookie(args){var cookie_string = args[0] + \"=\" + escape(args[1]);alert(cookie_string);switch (args.length){case 3:cookie_string += \"expires=\" + args[2];break;case 4:cookie_string += \"path=\" + args[3];break;case 5:cookie_string += \"domain=\" + args[4];break;case 6:cookie_string += args[5];break;}document.cookie = cookie_string;}";
private static var get_cookie:String = "function getCookie(name){var allcookies = document.cookie;var pos = allcookies.indexOf(name + \"=\");if (pos != -1) {var start = allcookies.indexOf(\"=\", pos) + 1;var end = allcookies.indexOf(\";\", start);if (end == -1) end = allcookies.length;var value = allcookies.substring(start, end);value = unescape(value);return value;}}";
private static var remove_cookie:String = "function removeCookie(name){var cookie = name + \"=\";cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';document.cookie = cookie;}";
public static function setCookie(name:String, value:String, expires:Date, path:String, Domain:String, secure:Boolean):Void
{
ExternalInterface.call(write_cookie, arguments);
}
public static function readCookie(name:String):Object
{
return ExternalInterface.call(get_cookie, name);
}
public static function removeCookie(name:String):Void
{
ExternalInterface.call(remove_cookie, name);
}
}
Code in the .FLA:
import flash.external.ExternalInterface;
CookieJar.setCookie("flash_cookie", "Hello World");
alert("FlashCall: " + CookieJar.readCookie("flash_cookie"));
CookieJar.removeCookie("flash_cookie");
alert("FlashCall: " + CookieJar.readCookie("flash_cookie"));
function alert(value)
{
ExternalInterface.call("alert", value);
}