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.

    Jen deHaan is a consultant specializing in instructional design, web design, WordPress, technical project management, and writing. She is based in the San Francisco Bay Area.

Tagged with: , , , , , , , , ,
Posted in ActionScript 2.0, ActionScript 3.0, buttons, Flash CS3, Flash CS4, Migration
  • Lucia

    okay what if I just want a script that would work on MKS to work here? like this one that would lead to another frame:
    on (release) {
    _root.gotoAndPlay(“”);
    }

  • http://www.flashthusiast.com Jen deHaan

    @Lucia: Not to sure what the MKS is, but your code is ActionScript 2.0 that should be placed on a button (select the button and add the code to it.) If you want to use the equivalent ActionScript 3.0 to do that, please see this post: http://flashthusiast.com/2008/02/25/making-a-button-work-in-flash-cs3-with-actionscript-30-its-not-too-bad

  • Mary Ann

    Hi Jen,
    I’m creating my first Flash website in CS4. I have what I think is called a splash page, with an enter button to navigate to the home page. I have an actionscript/label layer, and all the pages have frame labels. The file works great, except I can’t figure out how to make the enter button on the splash page link to the home page.

    I’ve tried using the same invisible button I used for the pages on the menu bar, with similar code, but it’s not working.

    How do you link a button in a Flash website to a different page in the same website?

    Thank you,
    Mary Ann

  • TomA

    How do I link a button on MouseOver to change the frame on another movieclip on the stage?

    Basically I want to change the background image on a rollover for each button with AS3.

  • Alan

    It works fine with one button, but when I try it with two buttons I get this error:
    (Location) Scene 1, Layer ‘Layer 2′, Frame 1, Line 2
    (Description)1021 Duplicate function definition (Description)
    (Source) (function myHandler(event:MouseEvent:void{

  • http://www.remcowebsites.nl Remco

    Alan, you have to rename the Myhandler function for every button.

    So button 1 has myHandler
    and button 2 uses myHandler2

    Those names may not be the same!

  • Schrene

    I am new to Flash. I have built websites and dynamic PDF’s so I am familiar with “coding”.
    I can not figure out why in AS3 I have to have my buttons on all of the pages in order for them to work. It seems some of the simplest things are what end up stumping me.

    I know how to make this work with AS2 but I am more interested in going forward with AS3.

    Any help would be VERY appreciated.
    :)

  • Schrene

    I knew it was easy!
    I just figured it out on my own.

    Thanks for the help LOL

  • Bret

    My “_self” command is not being recognized or is being interpreted as “_blank” as each button opens a new page instead of the requested URL into the existing window.

    No errors reported and the links open just fine,… only in a new window every time.

    Could it possibly be because I’m trying to navigate to a another page within the same directory instead of a full URL?

    Here is a sample code:
    function goTeamMembersPage(event:MouseEvent):void {
    navigateToURL(new URLRequest(“teammembers.html”), “self”);
    }
    History.addEventListener(MouseEvent.MOUSE_UP, goHistoryPage);

  • Bret

    Nuts. Copied the wrong segment of code. Should have been this:

    TeamMembers.addEventListener(MouseEvent.MOUSE_UP, goTeamMembersPage);

    function goTeamMembersPage(event:MouseEvent):void {
    navigateToURL(new URLRequest(“teammembers.html”), “self”);
    }

  • Sue Confused

    I hate coding….am so used to desktop authoring that I’m spoiled rotten. All I’m trying to do is incorporate a back, next, and exit button but can’t seem to get the action script written correctly. Can someone help?? Thanks so much.

  • mehdi

    Want vision video

  • riad

    i’m sending you this message and asking your help in something a bit easy for you,
    if you log in to this website: http://www.bay-tower.com
    you’ll see how the menu and the associated pages is working: moving forward, when clicking another it goes backward and the clicked one forward etc…
    how can we do it with minimum effort plz,
    waiting your reply
    Regards

  • Mary

    Hi,
    No matter what I can not stop my flash file from looping. It seems like the simplest command. I’ve tried “stop();” “this.stop()” “goToandStop();” This is extremely frustrating. Any ideas why this won’t work?

    Thanks,
    Mary

  • matilda

    HELP!!!
    PLEASE:)

    Hello,
    I need help with some actionscript!;)

    my slideshow is almost workin but i am getting an error message saying:

    1084: Syntax error: expecting rightparen before SimpleButton.

    Here is what my code looks like:

    stop();
    //next button
    SimpleButton.addEventListener(next_btn.onPress=function(){
    if(studio_art1._currentframe==studio_art1._totalframes){
    studio_art1.gotoAndStop(1)
    }else{
    studio_art1.nextFrame()
    }
    }

    //last button
    SimpleButton.addEventListener(back_btn.onPress=function(){
    if(studio_art1._currentframe==1){
    studio_art1.gotoAndStop(studio_art1._totalframes)
    }else{
    studio_art1.prevFrame()
    }
    }

  • Diipole

    Thanks for that! Finally the right info.

  • Eleya

    Okay, sorry for my messy msg but I really do need help.. HELP! I found that nice free template I’d love to use:
    http://www.flashmo.com/preview/flashmo_132_horizon

    Can I add “getURL” buttons (more like menu) to this layout via CS3/CS4? I have products I’d like to show like this and I have ~5 different categories. :) Anyone can help me?

    THE CODE:

    stop();
    import fl.transitions.Tween;
    import fl.transitions.easing.*;

    var filename_list = new Array();
    var url_list = new Array();
    var url_target_list:Array = new Array();
    var title_list = new Array();
    var description_list = new Array();

    var i:Number;
    var tn:Number = 0;
    var scale_factor:Number = 0.65;
    var rollover_duration:Number = 0.4;
    var motion_duration:Number = 0.8;
    var dragger_tween:Tween;

    var half_of_tn_width:Number = 80;
    var new_position:Number;
    var total:Number;
    var flashmo_xml:XML = new XML();
    var folder:String = “thumbnails/”;
    var xml_loader:URLLoader = new URLLoader();
    xml_loader.load(new URLRequest(“flashmo_129_thumbnail_list.xml”));
    xml_loader.addEventListener(Event.COMPLETE, create_thumbnail);

    var thumbnail_group:MovieClip = new MovieClip();
    stage.addChild(thumbnail_group);

    thumbnail_group.mask = tn_group_mask;
    thumbnail_group.x = tn_group.x;
    thumbnail_group.y = tn_group.y;

    tn_group.visible = false;
    tn_title.text = “”;
    tn_desc.text = “”;
    tn_url.text = “”;

    function create_thumbnail(e:Event):void
    {
    flashmo_xml = XML(e.target.data);
    total = flashmo_xml.thumbnail.length();

    for( i = 0; i < total; i++ )
    {
    filename_list.push( flashmo_xml.thumbnail[i].@filename.toString() );
    url_list.push( flashmo_xml.thumbnail[i].@url.toString() );
    url_target_list.push( flashmo_xml.thumbnail[i].@target.toString() );
    title_list.push( flashmo_xml.thumbnail[i].@title.toString() );
    description_list.push( flashmo_xml.thumbnail[i].@description.toString() );
    }
    load_tn();
    }

    function load_tn():void
    {
    var pic_request:URLRequest = new URLRequest( folder + filename_list[tn] );
    var pic_loader:Loader = new Loader();

    pic_loader.load(pic_request);
    pic_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_loaded);
    tn++;
    }

    function on_loaded(e:Event):void
    {
    if( tn tn_group_mask.x + half_of_tn_width )
    new_position = tn_group_mask.x + half_of_tn_width;
    else if( new_position < tn_group_mask.x – thumbnail_group.width + tn_group_mask.width )
    new_position = tn_group_mask.x – thumbnail_group.width + tn_group_mask.width;

    new Tween( thumbnail_group, “x”, Strong.easeOut,
    thumbnail_group.x, new_position, motion_duration, true );
    }

    var on_drag:Boolean;
    var diff:Number = ( drag_area.width – dragger.width ) * 0.5;

    dragger.x = drag_area.x;
    dragger.y = drag_area.y;
    dragger.addEventListener( MouseEvent.MOUSE_DOWN, drag);
    dragger.addEventListener( MouseEvent.MOUSE_UP, drop);
    dragger.addEventListener( MouseEvent.MOUSE_OUT, drop);

    function drag(me:MouseEvent):void
    {
    on_drag = true;

    if( dragger_tween )
    dragger_tween.stop();

    dragger.addEventListener( Event.ENTER_FRAME, update);
    }

    function drop(me:MouseEvent):void
    {
    on_drag = false;
    dragger_tween = new Tween(dragger, “x”, Strong.easeOut, dragger.x, drag_area.x, motion_duration, true);
    }

    function update(e:Event):void
    {
    if( on_drag )
    {
    dragger.x = stage.mouseX;

    if( dragger.x – drag_area.x diff )
    dragger.x = diff + drag_area.x;
    }
    else if( dragger.x – drag_area.x == 0 )
    {
    dragger.removeEventListener( Event.ENTER_FRAME, update);
    }

    thumbnail_group.x -= ( dragger.x – drag_area.x ) * 0.6;

    if( thumbnail_group.x > tn_group_mask.x + half_of_tn_width )
    thumbnail_group.x = tn_group_mask.x + half_of_tn_width;
    else if( thumbnail_group.x < tn_group_mask.x – thumbnail_group.width + tn_group_mask.width )
    thumbnail_group.x = tn_group_mask.x – thumbnail_group.width + tn_group_mask.width;
    }

  • Dave

    I have file that stops at a spot in the flash that has 5 buttons those 5 buttons go to 5 different frames (that also have stops) those frames have a button that links to a url I tried the code supplied here…
    my_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
    function myHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.adobe.com/devnet/flash/”), “_self”);
    }
    but when I duplicate it for the other four buttons I get an error
    Description: 1021: Duplicate function definition, Source: function mousedownhandler(even:MouseEvent):void{
    Anyone got a solution?

  • Jared Schwalbe

    Alright, I’m working on making a button open an html page in the same window. I’m using CS3 and actionscript 3 and this is my code:

    my_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
    function myHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.adobe.com/devnet/flash/”), “_self”);
    }

    Now when I use it in the actual Flash file I want it in, I get no error message but it doesn’t work. However, when I open a new file and try it, it magically works. I’m so confused! Help?

  • Jared Schwalbe

    I figured it out, I just needed to put it in the main timeline. However this did propose a new problem. When it’s in the browser and I click on the button that should take me to a url, a pop window pops up and says “adobe flash player has stopped a potentially threatening problem.” and goes on about how it’s trying to communicate with the internet and that I need to change my settings. I don’t want user to see this, so how can I fix it?

  • Cat

    Thanks! was able to get my button to work!

  • http://www.meta-level-marketing.com Kurt

    I am a Noob to Flash and am having problems getting some movieclips to link to different URLs on release. I am able to get them to animate, and I am able to get them to link to an html page in the same folder, but I can’t get both functionalities to work at the same time and it is driving me crazy!! I’ve tried putting the actionscript in the instance object, in the action layer, and other things and nothing seems to work!?

    Here is the link to the source file: http://www.myfavorites.com/flashtest/header_v8_122209_v2.fla
    I believe that the actionscripting needs to be done in the “baners” movieclip, but it could also be the “Symbol 3″ movieclip. I’m not sure and this is where I also need help.

    Here is also a link to the .swf (http://www.myfavorites.com/flashtest/header_v8_122209_v2.swf). See, if you mouse over the first two clips, they animate, but they do not go to the HTML page if you click on them (http://www.myfavorites.com/flashtest/success1.html). The third clip will go to the page success1.html in the same folder if you click on it, but it does not animate on mouse over.

    I’m using Flash CS3 as my editor and I’m using ActionScript 2.0 for the scripting.

    Any help you can provide on this is most appreciated!! I’m normally pretty good at figuring out things on my own, but This problem is driving me crazy!!

  • Farhan

    Hi,
    I am new to AS3, the problem i am facing is when i click on the frame the “action panel” where we add the coding – it works fine, but when i add or make a button the “action panel” is not working
    i can’t add any coding in it, like it gets disabled.

    why does this happens.

    Thanks

  • cassia

    Working well thanks

  • Jake

    can someone pls pls help me with linking my button to open in the same window. it is soooooo weird because with my first button (menu) it works but with my second, it doesnt work although every code is the same. it only works with _blank. with _self, if u click the button, nothing happens! nothing appears!/ i named my instance, put my actionscript in a different layer in the same frame, put

    my_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
    function myHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.adobe.com/devnet/flash/”), “_self”);
    }
    I made one/ im trying to make another one. but it doesnt work !!! im so frustrated by it. pls help me solve this problem!~

  • gabby

    please help!
    I am making a flash portfolio site and i have many different buttons. i want them all the open on “_self”, but even though i renamed the handler function for each button, all the functionality stops working and clicking anywhere on the screen will only take you to the last buttons’ URL! If anyone knows why…please reply.

  • http://www.fidelitypayment.com/node/85 James Gray-King

    Hi all, some help would be gratefully received! I have used the code

    fraud_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
    function myHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.fidelitypayment.com/node/25″), “_parent”);
    }

    and it is working fine when i test the movie. However, when uploaded to http://www.fidelitypayment.com/node/85 (my testing space) it ceases to work at all, any ideas?

    Many thanks!!!

  • http://www.grovegraphix.com/ Kim – graphic designer and web designer

    Hi,

    I’m trying to make 4 different tweened buttons in a flash banner link to 4 different urls on a website. I can make the first one work, but this is the error message that I’m getting on the second one.

    Attemping to launch and connect to Player using URL /Users/kimd/Sites/grovegraphix/directcableresource.com/xhtmlcss/images/homepage_flash/home_flash_3.swf
    [SWF] Users:kimd:Sites:grovegraphix:directcableresource.com:xhtmlcss:images:homepage_flash:home_flash_3.swf – 140300 bytes after decompression
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at home_flash_3_fla::MainTimeline/frame1()[home_flash_3_fla.MainTimeline::frame1:9]

    This is the AS3 code that I have:

    //Go to the grovegraphix.com website
    fiberjumpercables_btn.addEventListener(MouseEvent.CLICK,gotoGrove);

    function gotoGrove(evtObj:Event):void{
    var theSite:URLRequest = new URLRequest(“http://www.grovegraphix.com/directcableresource.com/xhtmlcss/landing_fiber_jumper_cables.html”);
    navigateToURL(theSite,”_self”);
    }

    audiovideocables_btn.addEventListener(MouseEvent.MOUSE_DOWN, myHandler);
    function myHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://www.grovegraphix.com/directcableresource.com/xhtmlcss/landing_audio_video.html”), “_self”);
    }

    The fiberjumpercables_btn is on frame 1.

    The audiovideocables_btn is on frame 16.

    I have 2 more buttons which I haven’t written code for yet.

    fiberaccessories_btn on frame 34 and outletplug_btn on frame 61.

    Sorry, I’m new to Action Script 3.0.

    Thanks in advance for your help.

    Kim

  • http://www.grovegraphix.com/ Kim – graphic designer and web designer

    Never mind, I got it figured out. Yah!! Here is the link.

    http://www.grovegraphix.com/directcableresource.com/xhtmlcss/index.html

    Click on each of the products and it will go to a landing page for that product.

    Kim

  • Sean – CF Web Programmer

    I have a CFForm with format=”flash” and I have a cfinput type=”button” within that CFForm. I am trying to place the event handler onClick into the button where, once clicked, a new window will appear. I have tried placing JS for the onClick and I have also tried AS. I can’t seem to get either to work. Can anyone help?

    Thanks,
    Sean

  • http://irisillustratie.nl Iris

    I would like my webpage to have the buttons open their urls in the bottom frame.

    Searched everywhere but I can’t find anywhere how to fix this with my Adobe Photoshop CS3. I got as far as adding URLs to everything through your website here, but now I don’t know how to open them in the frame ‘bottom’. If I add ‘_bottom’ it doesn’t work. I hope someone here can help me…

    Crappy test site to show what I mean: http://punkyfox-design.com/flash/index2.htm (clicking the buttons opens the urls in new pages, when it should open them in the ‘bottom’ frame.)

  • ZHENNING

    Hi

    Need some help here.. I created a drop down menu in AS3 as movie clip and each drop down has multiple links that i would like to link it to different frames..but i don’t know how to link them. Anyone can help? Thanks!

  • AP

    I am loving your posts, they are simple, quick, and live-saving. Thanks!!

  • maria

    Hello i have a site in action script 3, and i have to create a simple button there same as get url in action script 2. I try to use the monkey code there

    monkey_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    function mouseDownHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest("http://www.flashthusiast.com/&quot ;) );
    }

    but something goes wrong, because when i click in the area around the button it works like clicking on the button.

    Please heeeelp me!!!

  • Cody

    I am really new to scripting (in any language) and I’m trying to look at all variables in the code and determine their meaning/purpose.

    What is the purpose of ” :void “?

  • Kyle

    I’m having an issue using CS4 and ActionScript3.0. How do I make this link open in a new window and not a new tab? It seems to work fine with IE but not Google Chrome or Firefox.

    listen3.addEventListener(MouseEvent.MOUSE_DOWN, thumbDownHandler);
    function thumbDownHandler(event:MouseEvent):void {
    navigateToURL(new URLRequest(“http://myprivatewebsite”));
    }

  • Chris

    Thanks so much for your EASY to understand pages, was having a hard time switching from AC2 to AC3. Was pulling my hair out trying to get my links to work. Two of your pages helped me more than the 30+ I was searching through for the correct answer! Thanks :)