views:

192

answers:

3

I have a flash movie using Actionscript 3 with some buttons that open links to new pages. Here is the code I have for redirecting to the new page:

myButton.addEventListener(MouseEvent.CLICK, function(e:MounseEvent) {
    var request:URLRequest = new URLRequest('http://www.example.com/page2.html');
    navigateToURL(request, "_top");
});

It works fine on my production server with the full url including domain, but when I change it to this:

var request:URLRequest = new URLRequest('page2.html');

it no longer works in production. What am I missing here? I would like to not have to encode the entire URL into the movie.

+1  A: 

Try using /page2.html

Chris Gutierrez
+4  A: 

URLRequests are relative to the HTML file containing them (it doesn't matter where your SWF is), so you need to take that into account. There is an optional attribute of the EMBED/OBJECT that you can set to change this behaviour, see here:

base - . or [base directory] or [URL]. Specifies the base directory or URL used to resolve all relative path statements in the Flash Player movie. This attribute is helpful when your Flash Player movies are kept in a different directory from your other files.

http://kb2.adobe.com/cps/127/tn_12701.html

EDIT: Also, try to avoid using absolute URLs for relative files, as you could get sandbox errors, for instance, loading the web without "www."...

Cay
+1  A: 

/page2.html should work fine as Chris suggested.

Also, one thing I often do for larger projects with lots of URL's is simply create a set of global link functions that I can switch between for different launch environments.

Where I work we have at least 4 or 5 different environments between production, staging, and various testing environments, and sometimes URL's have to be tweaked (for example, if a data source is on staging and not on production yet).

So I would make a high level function like

MakeURL()

Which I would feed a base page string such as "page2.html", and the MakeURL function would parse in a URL prefix such as http://staging.site.com or http://alpha.site.com.

That way if I had to, I could change every link in the app quickly.

Now, you rarely need to put the entire URL in there unless you are working with multiple and varied environments. Usually /page2.html will work, but I use that function sometimes as a failsafe.

Jasconius
This is always very handy... In a recent project I extended the idea to add a sufix with a version number (?v=1.0.3) in the testing and production environment to avoid caching unwanted files... this way, everytime something changed in a sensible file (that could break something using older files) I would change the version number (as a parameter of the main swf), so the user would download a fresh set of all files and cache those till the next core iteration. It turned out to be really useful, specially when checking changes with the client :)
Cay