Controlling the window target a button click opens using ActionScript 3.0 in Flash

One of my frequently asked questions in the comments is how to control what window a button click opens using ActionScript 3.0. For example, you might want your button to open in the same window, a particular part of your frames layout, or a new window. It’s similar to ActionScript 2.0, but there is a bit of a difference.

In ActionScript 2.0, you set a target for your button using code such as the following:

The following steps for ActionScript 3.0 will apply to either Flash CS3 or Flash CS4, just make sure that your document is an ActionScript 3.0 button and you’re adding the code to a frame.

  1. Create your button in your FLA, and give it an instance name in the Property inspector, such as my_btn.
  2. Select a frame on the timeline at the same frame number of your button.
  3. Open the Actions panel, and add the following code:
    my_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
    function myHandler(event:MouseEvent):void {
        navigateToURL(new URLRequest("http://www.adobe.com/devnet/flash/"), "_self");
    }
  4. Publish preview your work in HTML (File > Publish Preview > HTML). Note: When testing locally, you will need to set your security to allow access to the SWF. If you haven’t set this up, click Settings in the dialog that opens when you first test your FLA, select a directory on your hard drive, and save the FLA to that same directory and test again.
  5. By default, without adding this parameter, ActionScript assumes _blank which opens a new window. You can modify the _self in the code above to be _top or _parent (or _blank if you want). Here’s what each of them does:

    • _self opens the link in the current frame of the current browser window.
    • _blank opens the link in a new window (or tab if that’s what the user has their browser set to do).
    • _parent opens the link in the parent of the current frame.
    • _top opens the link in the top-level frame of the current browser window.
40 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

WP-SpamFree by Pole Position Marketing