CachedSharedObject Class, A Local Shared Object that clears with the browser cache.
by Brian Hodge on Apr.01, 2009, under Actionscript 3.0
DEMO: http://dev.hodgedev.com/DeSOL_testing/
Note: This is not intended as a replacement for SharedObject. Obviously if you still require some level of persistence you could just use the native SharedObject; However, if you require the use of a SharedObject and would like it to clear with the browser cache, or by a certain date, this class can help you.
The class works by creating a cookie when the shared object is created; This is done with the help of the ExternalInterface Class in the flash.external Package which makes a call to a javascript function.
From the point of creation, the SharedObjects lifespan is directly related to that of the Cookie’s life span. The two share the same name, and upon load of the page a function is called to check if a particular cookie exists. The cookie acts as a reference for the SharedObject. If the cookie is removed, meaning the cache has been cleared, the next page load checks the cookie, finds that it does not exists, and clears the SharedObject that has the same name.
-
- //The AS3 Class
- package com.hodgedev.desol.net
- {
- /**
- * …
- * @author Brian Hodge (brian@hodgedev.com)
- */
- {
- /**
- * creates a cookie via ExternalInterface and a call to a
- * javascript function which is expecting three things:
- * a name, a value, how many days until expiration. A
- * SharedObject and a cookie are created with the same name.
- * The SharedObjects life is dependent up on cookie's life.
- * @param pName string used to name the SharedObject
- * @return SharedObject
- */
- {
- var createStr:String = ExternalInterface.call("createCookie", pName, "Exists to allow flash local shared objects to clear with browser cache clear.", "7 " );
- }
- /**
- * an array of cookies recieved from the javascript window.cookies
- * call returned as a string then converted to an array.
- * @return
- */
- {
- }
- /**
- * Acts as the garbage collection for a CachedSharedObject.
- * The cookie that was created at the same time is the
- * reference. If the reference no longer exists, hence
- * the cache was cleared, the SharedObject is removed.
- * @return Boolean
- */
- {
- if (CachedSharedObject.cookies().length < = 1 )
- {
- trace('No cookies found, clearing all CachedSharedObjects.');
- return true;
- }
- return false;
- }
- /**
- * reads a cookie via ExternalInterface. A
- * javascript function returns a string which
- * this function then returns a reformmated
- * version of.
- * @param pName
- * @return String
- */
- {
- return "Cookie name: " + pName + "\nCookie value: " + readStr;
- }
- /**
- * Erases a cookia via ExternalInterface. A
- * javascript function deletes the cookie.
- * @param pName
- */
- {
- }
- }
- }
- //The javascript
- /**
- *Author URL: http://www.quirksmode.org/js/cookies.html
- *Notes: Thanks to the author at the above site for the
- *easy to use functions.
- */
- function getCookies()
- {
- return unescape(document.cookie.toString());
- }
- function createCookie(name,value,days)
- {
- if (days)
- {
- date.setTime(date.getTime()+(days*24*60*60*1000));
- var expires = "; expires="+date.toGMTString();
- }
- else var expires = "";
- document.cookie = name+"="+value+expires+"; path=/";
- return "Cookie created.";
- }
- function readCookie(name)
- {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++)
- {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1,c.length);
- if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
- }
- return null;
- }
- function eraseCookie(name)
- {
- createCookie(name,"",-1);
- }
April 3rd, 2009 on 7:18 pm
Great site this blog.hodgedev.com and I am really pleased to see you have what I am actually looking for here and this this post is exactly what I am interested in. I shall be pleased to become a regular visitor