开发者

Relative URLs in Actionscript 3

开发者 https://www.devze.com 2022-12-09 03:58 出处:网络
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:

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.


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."...


Try using /page2.html


/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.

0

精彩评论

暂无评论...
验证码 换一张
取 消