Detect if the swf is being executed localy or online

April 2nd 2009

Ends up as a very simple line, but took a bit to figure it out.

var localMode : Boolean = loaderInfo.url.indexOf("file") == 0;
19 comments written so far...

Helpful. I usually ended up checking to see if my FlashVars were set to null or not. This is a lot mo' betta. Thanks.
April 2nd 2009
hebchop
Ditto.. just tried it against a check on my local dev server

var localMode : Boolean = loaderInfo.url.indexOf("http://localhost") == 0;

April 2nd 2009
Shane
I used to use that, untill a colleage pointed me to: Capabilities.playerType.


Greetz Erik
April 2nd 2009
Erik
This is what I use:

trace(System.capabilities.playerType);

returns:

"StandAlone" for the Flash StandAlone Player
"External" for the Flash Player version used by the external player, or test movie mode
"PlugIn" for the Flash Player browser plug-in
"ActiveX" for the Flash Player ActiveX Control used by Microsoft Internet Explorer
April 2nd 2009
reyco1
return new LocalConnection( ).domain == "localhost";

More domain functions can be found on:

http://code.google.com/p/braemar/source/browse/trunk/src/org/braemarframework/utils/DomainUtils.as
April 2nd 2009
Patrick Pietens
var localMode : Boolean = loaderInfo.url.indexOf("http:\\www.file.com") == 0;
April 2nd 2009
deep
an another way:

static public const LOCAL:Boolean = Security.sandboxType != Security.REMOTE;
April 2nd 2009
lab9
@Patrick Pietens

then check for 'file:///'.. Thats how we used to do it.
April 3rd 2009
Fedlarm
or you could just unplug the cable and see if it works :P

var noob:NoConnection = Cabble.status();
April 3rd 2009
am (noob)
I second the playerType approach, BUT that technically only determines if you're in a browser or not (and if not, that usually means the Flash IDE, but I suppose it works for stand-alone debug mode). It's possible you just opened up the HTML page containing the SWF in a browser, without it being served up on HTTP. In that case, the "indexOf file" trick still correctly determines if you are local or not.

I depends on your needs, I guess. Do you want to determine if you're in a browser or not, or if you're on a web server or not?
April 3rd 2009
Dru
Along those lines, I often have a Main movie and a Preloader movie, and I want the Main movie to run either by itself (for debugging) or inside a Loader when it's preloaded. So I kick everything off for Main through a public init() function, and in Main's constructor, I check:

if( !loaderInfo.loader ) {
init();
}

April 3rd 2009
RoomsInJars
if (Capabilities.playerType != 'ActiveX' && Capabilities.playerType != 'PlugIn' ) {
}
works great
April 4th 2009
marpi
var localMode : Boolean = loaderInfo.url.indexOf("file") == 0;

What happens if your SWF file URL is something like: "http://mywebsite.com/files/foo.swf"?

If you use this option you would be safer to do it like:
var localMode : Boolean = loaderInfo.url.indexOf("file://") == 0;

April 6th 2009
skorin
If the URL were "http://mywebsite.com/files/foo.swf" then indexOf will return 21 ;)
April 13th 2009
mr.doob
Hmm. each and every method above had an issue...

loaderInfo.url.indexOf("file") == 0;
TypeError: Error #1009: Cannot access a property or method of a null object reference.

trace(System.capabilities.playerType);
Warning: 1058: Migration issue: The property capabilities is no longer supported. The capabilities class has been renamed Capabilities..

trace(System.Capabilities.playerType);
ReferenceError: Error #1065: Variable System::Capabilities is not defined.

trace( Security.sandboxType , Security.REMOTE );
ReferenceError: Error #1065: Variable Security is not defined.
April 19th 2009
mr noob
Are you trying them on flash5 or something? :P
April 19th 2009
mr.doob
fixed, was trying to use them within a class. works just fine on the timeline before the class initialises, so that's probably the best place to be doing this anyway.
April 20th 2009
mr noob worked it ou
This doesn't help against Crackers that try to hack my highscore does it? ..well maybe it's a little obstacle more.
April 29th 2009
tapete
PlayerType is fine if you are testing in CS4 in the Flash player but in Flex you'll be in the browser ... Dooby's method is the simplest way of detecting if you're online unless you are deploying to a server.
May 12th 2009
Lee Probert

Have your say!

Name:

Website:

Comment:

Some of the projects that I worked on.



Some of the HTML5 and Actionscript experiments I've done.