ActionScript 3: How to get hostname from URL?

I am trying to set the path to an XML file that is different when running locally or on the server.

How to determine if Flash is running locally? (I'm going to check if the url contains " http: // localhost ", but how do I get the window url?)

Thank you in advance

+4
source share
4 answers

mokee is right in that you need to specify a relative path to access your XML when working locally. You must map this folder structure to the server as well. However, it is often necessary to connect different base paths before this relative path to point to specific versions of these folders. For example, we often have several different conditions in our agency. developers testing servers, different live environments. You do not want to hardcode them, plus you need to somehow determine which one is actually used.

The solution to this problem is often passed in basePath as FlashVar

Take a look at this solution on how to pass FlashVar to your swf

http://blogs.adobe.com/pdehaan/2006/07/using_flashvars_with_actionscr.html

Set up a flash version pointing to basePath on your server. Sort of

FlashVars="basePath=http://www.yourdomain.com/flash" 

Then in ActionScript you can check if this basePath exists. If this is not the case, then you know that you work locally, so you only use the relative path that you created that works locally. If there is basePath, then prefix this in front of your relative path and load the xml using this url.

Very often this is done and works very well.

+3
source

You can request an external window (container) for this information through an external API

 if (flash.external.available) // An external window is not always available { var win:* = flash.external.call("window.location"); // evaluate window.location // depending on browser there are several properties like 'host' you can use // to get the actual hostname // eg win.host == "localhost" } 

If the above method does not work, you need to create a wrapper function to return this window.location object from JavaScript. Please note that the properties available in the win object are fully customizable by the browser and vary between browsers. It also means that the flash player (which you get if you run the movie hosted by the flash player outside the browser environment will not know about the window.location property, this works when you deploy your movie, but not when debugging)

More details here.

+6
source

If your application is a Flex application, you can get the URL of the main SWF file using the url attribute of your application object. This can be obtained from anywhere in your controller tree through parentApplication.url or, if the object is not part of the controller tree, Application.application.url .

For simple AS3 projects, you need to access the loaderInfo attribute of your main sprite, for example, _root.loaderInfo.url will provide you with the same information.

+4
source

Set the path to the absolute relative XML value: '/temp/data.xml'. Thus, he will search for XML at: http://yourhost.com/temp/data.xml '.

If it is set to "temp / data.xml", it will always look for it relative to SWF.

if you are happy and you know this, data.xml.

+1
source

All Articles