views:

8645

answers:

7

Hi,

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

+2  A: 

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

John Leidegren
A: 

Thank you John.

Sorry I should've mentioned I am very new to AS3 and I am not really sure what I am doing.

The XML file is actually located relative to the .swf file. Currently I got

var xmlfile = "temp/data.xml";

The .swf file could be included on any page, eg. on both of these pages
http://myhost.com/index.html
http://myhost.com/about/company.html

If the swf is at http://myhost.com/flash.swf, the XML file would be at http://myhost.com/temp/data.xml

My problem is that this only works only if:
- run locally, AND
- viewed on http://localhost/mywebsite/index.html (index.html is in the same directory as flash.swf)

When run from other directory, or on the live server, it doesn't seem to find the XML file.

Do you have a solution for this?

Thank you very much.

Edit the original question instead of posting answers and comment, this is the preferred way of following up feedback on stackoverflow.com, anyway I think you'll find your answer here. You might wanna read up on how flash resolves relative URLs and what security policies apply.
John Leidegren
Flash can be a pain in that it will not resolve URLs which look strange to Flash which can be mostly annoying. You should find an answer in any of the other ones given here. This looks like one of those silly mistakes where you aren't really doing anything wrong, it's just some stupid config thing.
John Leidegren
+1  A: 

Hi aximili

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.

monkee
+1  A: 

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.

James Hay
Perfect! Thank you James
+1  A: 

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.

David Hanak
Sorry it's not a Flex application
@aximili: No problem, just read on, the second paragraph covers that case.
David Hanak
A: 

David... loaderInfo.url or loaderInfo.loaderURL only gives you the url of the SWF... which is not what is needed...

rp

A: 

It's total wrong.

Nothing code is workig and in AS3 _root.loaderInfo.url never used

if you use loaderInfo.url then it will return the SWF uploaded location not the browser location.