Going from Flash to any URL

Perhaps you have an application that starts off as Flash, but then you want to go to a specific HTML page. Flash ActionScript has a command called navigateToURL that will accomplish this task.

The command takes two parameters. The first parameter is a URLRequest object that defines the URL. This is simpler than it may seem. The fragment

new URLRequest(" creates a URLRequest object to be used by navigateToURL using a string holding the name.

The second parameter specifies what window is to be used for the URL. The default is a new window, distinct from the window running the Flash program. The value to get this effect is "_blank", that is, if you do not rely on the default value, make "_blank" the second parameter. Other options are "_parent", "_self", and "_top". These options have distinct effects when the original window, the one running the Flash program, is a frame. The window for "_blank" will not have a working back button in the browser—it is a new window! The others will. You can experiment with these others. NOTE: when testing your application in the Flash development environment, all options produce new windows. You need to publish and test the published application to see the differences.

I created two examples. In both applications, I created a sequence of keyframes to represent a simple animated sequence. On the 36th frame, also a keyframe, I put in 3 button components, gave each names (jmbtn, nybtn, and fivebtn) and each a distinct label. Now, my goal here is to have the event handling for the buttons result in navigation to 3 distinct URLs. How to do this? One approach is to use the addEventListener method for each button to set up a call to 3 distinct functions. That would be fine. However, here is an alternative approach. ActionScript provides a way to define a function—not reference or give a pointer to a function, but actually include the whole definition of the function as a parameter for the addEventListener method. It is called defining an anonymous function. Why anonymous? These functions do not have names. However, defining them this way does set them up for being invoked when the event happens. See also the Tutorial for Rock Paper Scissors for a similar example.

The functions I define for this application are each calls to one specific function, a function I define called goto. The difference is in the parameter to the function goto. In the Actions panel for this frame, I put in the following code:

jmbtn.addEventListener(MouseEvent.CLICK, function (ev) {

goto("../");});

nybtn.addEventListener(MouseEvent.CLICK, function (ev) {

goto("

fivebtn.addEventListener(MouseEvent.CLICK,function (ev) {

goto("

function goto(where:String):void {

navigateToURL(new URLRequest(where),"_blank");

}

stop();

The first 3 statements set up event handling for each of the buttons. Again, the event handling is a function defined right in the call. This function invokes the goto function with a specific parameter. Notice the use of { and } within the addEventListener call.

Getting back to the specifics of this application, notice that the parameter for the jmbtn button is a relative address and the parameters for the other buttons are absolute addresses. Relative addresses refer to the current location; absolute addresses are complete—they have the .

The goto function is defined to have 1 parameter: a string. The one statement within the function uses this string to construct a URLRequest object used as the first parameter to the navigateToURL command. The second parameter is "_blank".

The last line of ActionScript in the frame is the stop command. This is necessary since without it, control will loop back to the first frame.

The second example sets up different values for the second parameter for the navigateToURL command.

The ActionScript in the 36th frame is the following:

jmbtn.addEventListener(MouseEvent.CLICK, function (ev) {

goto("../","_blank");});

nybtn.addEventListener(MouseEvent.CLICK, function (ev) {

goto("

fivebtn.addEventListener(MouseEvent.CLICK,function (ev) {

goto("

function goto(where:String,win):void {

navigateToURL(new URLRequest(where),win);

}

stop();

NOTE again: the differences will not be detectable when testing in the Flash development environment.

Keep in mind that you can use the navigateToURL anywhere. For example, if your code determines that play is over in a game, you can go to a specific URL or to a calculated URL selecting from an array, perhaps, of strings representing different addresses. You may choose to include this goto function or something like it.

I made the decision to create and use the separate function that I named goto. I could have done all the work in the anonymous function. For example, the code for the first example could have started with

jmbtn.addEventListener(MouseEvent.CLICK, function (event) {

navigateToURL(new URLRequest("../"),"_blank");});

Making a distinct function seemed better, but it is debatable. One advantage is that if I decide to add an additional feature to the event handling, it will be easier to do.