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.

_online
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");
}

antiCache
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
my_mc.loadMovie(antiCache("file.swf",true));
*/
_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);
}