Aximili
Aximili

Reputation: 29464

ActionScript 3: How to get host name from URL?

I am trying to set a path to an XML file, which differs when run locally or on server.

How do I determine if the Flash is running locally? (I am thinking to check if the URL contains "http://localhost" but how do you get the window URL?)

Thanks in advance

Upvotes: 4

Views: 21664

Answers (4)

David Hanak
David Hanak

Reputation: 10974

If your application is a Flex application, you can retrieve the URL of the main SWF file via the url attribute of your application object. This can be accessed from anywhere within your controller tree via parentApplication.url, or, if an object is not part of the controller tree, Application.application.url.

For plain AS3 projects, you need to access the loaderInfo attribute of your main sprite, e.g., _root.loaderInfo.url will give you the same information.

Upvotes: 4

James Hay
James Hay

Reputation: 12700

mokee is right in that you need to specify a relative path to access your XML when running locally. You should match this folder structure somewhere on the server as well. However there is often a need to to attach different base paths in front of this relative path to point to specific versions of these folders. For instance we often have several different environments at our agency. dev builds, testing servers, different live environments. Your don't want to have to hard code these in plus you need some way to determine which one is actually being used.

The solution to this problem is often to pass in a basePath as a FlashVar

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

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

Set up a flash var pointing to a basePath on your server. Something like

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

Then in the actionscript you can check to see if this basePath exists. If it doesn't, then you know you are running locally, so you use just the relative path you have set up which works locally. If there is a basePath, then prefix this to the front of your relative path and load the xml using this URL.

It is very common to do this and works really well.

Upvotes: 3

user56725
user56725

Reputation:

Set the path to your XML absolute relative: '/temp/data.xml'. Like that it will search for the XML at: 'http://yourhost.com/temp/data.xml'.

If it's set to 'temp/data.xml' it will allways search for it relative to the SWF.

if you're happy and you know it, data.xml.

Upvotes: 1

John Leidegren
John Leidegren

Reputation: 60987

You can ask the external window (container) for this information through the 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
    // e.g. win.host == "localhost"
}

If the above method doesn't work you need to create a wrapper function for returning that window.location object from JavaScript. Note that what properties that are available on the win Object is entirely set by the browser and varies between browsers. This also means that the flash player (which you get if you run the movie hosted by the flash player outside of a browser environment will not know about the window.location property, this works when you deploy your movie but not when debugging)

Check here for more information

Upvotes: 6

Related Questions