_online and antiCache code snippets

These two source code snippets are great for when you’re developing Flash content for delivery online, that will also have an offline component, or that you will be testing locally.
The first is a simple one liner that sets up a global _online property that indicates whether the swf is being accessed via a web-browser online. The second is a more complex bit of code that allows you to intelligently control caching for loaded assets in a swf (swfs, jpgs, xml files, etc).
If you have any questions on the code, please enter them in the comments.
Read on for the code…

Hopefully this code copies in ok. Let me know in the comments if there are any problems.
Simply drop this code into frame one of your swf:

_online from FlashOS2 [ http://www.flashos.net ] by Grant Skinner [ http://www.gskinner.com/ ]
Just a simple snippet that sets a global _online property, indicating
whether the content is being accessed via the web
_global._online = (this._url.subStr(0,7) == "http://");
// Example usage:
if (_online) {
    trace("I'm online");
} else {
    trace("I'm being accessed locally");

This code requires that the _online property described above be available.
Simply copy the following code onto a frame early in your swf. You can then use it to intelligently control the caching of assets loaded into your swf by passing their urls through the antiCache function. If the root swf is online, antiCache will append a querystring variable named “antiCache” containing a random number. Read the code comments for more details, and examples.

/* ANTICACHE function from FlashOS2 [ http://www.flashos.net ]
by Grant Skinner [ http://www.gskinner.com/ ]
Appends a random number to the end of a url if the swf is being accessed via
the web, to prevent loading cached files. If AllowCache is true then the
number is stored, and applied to subsequent requests for the same file during
the current session. This allows it to load the file fresh each session, but
cache it for the duration of that session.
REQUIRES the _online property to work!!
Example 1:
MyURL = antiCache("http://www.gskinner.com/test.asp?ID=4");
// returns "http://www.gskinner.com/test.asp?ID=4&anticache=12312445"
Example 2:
MyURL = antiCache("http://www.gskinner.com/test.asp");
// returns "test.asp?anticache=12312445"
Example 3: loadMovie
_global.$antiCache_index ={};
_global.antiCache = function(p_URL,p_AllowCache) {
if (!_online) { return p_URL; } // REQUIRES the _online property to work!!
var num = null;
if (p_AllowCache) {
num = OS.Core.$antiCache_index[p_URL];
if (num == null) {
num = getTimer()+"."+random(1000000);
OS.Core.$antiCache_index[p_URL] = num;
} else {
num = getTimer()+"x"+random(1000000);
if (p_URL.indexOf("?") > -1) { p_URL += "&"; }
else { p_URL += "?"; }
return (p_URL+"anticache="+num);

Grant Skinner

The "g" in gskinner. Also the "skinner".



  1. Nice and easy, oh how you please me.

    Oh by the way this blog looks f’d up in Apple Safari

  2. Yeah, this entry probably looks f’d up in all browsers – it was either that, or make the code mostly unreadable.

    Glad you like the function though. 🙂

    Hopefully I can get Sean Voisen’s code beautifier to work soon.


  3. it does NOT work for gModeler with the new Flash Player 7. gModeler works just fine “locally”. 😀

  4. Not sure what you’re referring to… gModeler appears to run fine for me and others in FP7.

    Could you please clarify?


  5. I meant the cached version of gModeler works fine in the new player even when offline, inspite of the “antiCache” code.

  6. An anticache function, great idea. I still tend to hack them together where needed. Here’s a little tip some might find useful, instead of using a random number, that can, mathematically speaking repeat itself, why not use a date stamp? It will never repeat itself unless two people access the same URL at the same second in the same timezone, and if they do that, chances are you want to deliver the same file!

    The added bonus comes when you read your server logs and can see the time of each request and the timezone it came from (hours of fun, promise ;o)

    time= new Date();



    output window:

    Tue Sep 9 06:38:54 GMT+0200 2003

  7. Pete – it actually uses a combination of a random number + the value of getTimer. The chance of anyone getting the same value for the same file on subsequent visits is virtually nil.

    I originally considered doing it the way you suggested, but the date value would have to be escaped, and it’s more CPU intensive than the method I used.


  8. I use a similar function to test whether my movies are running online or not. I would recommend just checking for it beginning with “http” instead of the “http://”. That way it will work on a secure protocol as well (i.e. “https://”).


  9. Virtually Nil…

    According to Douglas Adams million-to-one chances crop up nine times out of ten.

    If the call is in the first frame, as it often is the getTimer method returns

    the same value every time, give or take a millisecond.

    So if theres 10000 cached requests on some proxy server”virtually nil”

    suddenly becomes 1 in a 100!

    CPU Intensive?

    I see that phrase used alot, when people really just mean “more code”.

    Realtime 3D worlds are CPU Intensive, this a feel is not.

    If you want less code you can drop the .toString;

    time=new Date();


    Loop the two methods, I’m sure you’ll see no difference in CPU usage if

    that’s what worries you.

    -Pete Mc

  10. Hi,

    I am producing a Miva Merchant store: http://www.fabfacescosmetics.com/Merchant2/merchant.mvc

    I am having a problem with IE caching the pages so that the basket and checkout keep coming up with old info. Works fine in Netscape.

    Your code looks promising although I am not much of a programmer so need a little help. If I understand correctly I need both of these programs. The first one is just dropped on frame one in my flash file.

    Where do I put the second program? Sorry to be so ignorant!

Leave a Reply

Your email address will not be published. Required fields are marked *