Home > Learn Flash > Flash Actionscript 3 URL link button

Flash Actionscript 3 URL link button

What:

To make a button with a link to an external webpage using Actionscript 3

Example:

Click on the “alexbrooke.com” button in the top left corner to navigate away from this page

To find out how to do this go to the next page:

How:

1) Draw your button

2) Select your button and click on MODIFY then Convert to Symbol

3) Choose button from the menu and click OK

4) Select the button if it isn’t selected. Now give it an instance name of “urlButton” in the small dialog box in the bottom left hand corner

5) Create a new layer and insert the following actionscript into it:

function gotoHomePage(event:MouseEvent):void
{
var targetURL:URLRequest = new URLRequest(“http://yourhomepage.com/“);
navigateToURL(targetURL);
}
urlButton.addEventListener(MouseEvent.CLICK, gotoHomePage);

6) Change the red url to whatever website you want to navigate to.

7) Publish the movie and see if it works

Extra stuff

The video player I used for this example was made by Rafael Nuenlist. You can view the tutorial here.

Please leave your comments, ideas and questions here.

Post to Twitter Post to Facebook Post to StumbleUpon

  1. Kevin
    June 16th, 2009 at 01:23 | #1

    Finally a straight-forward tutorial without any hitch and without any code problem. It works. I checked all the other tutorials for url buttons in AS3 and they ALL had some kind of problem: something assumed, missing code, incorrect code, etc.
    Thanks you

  2. admin
    June 17th, 2009 at 17:04 | #2

    Well, basically, these tutorials are for me which means they don’t assume you know anything about actionscript or flash. Glad you liked the post

  3. June 17th, 2009 at 17:54 | #3

    Hi, thank you for your script, just one question, when trying it locally it gives the hand indicator for a link, but when trying from the actual site I placed the swf, my button doesn’t show me the hand indicator. any ideas? thank you.

  4. admin
    June 17th, 2009 at 23:53 | #4

    Hmm… strange. Got the URL and code?

  5. Mark
    June 18th, 2009 at 03:35 | #5

    Hi…perfect code, but how do I get it to target=”_self”? It seems to be defaulting to open a new window. I need it to remain in the same window. When I add “, “_self” I get an error saying there are too many functions :(

  6. admin
    June 18th, 2009 at 16:19 | #6

    Try adding “_self” to the navigateToUrl function. Like this;

    function gotoHomePage(event:MouseEvent):void
    {
    var targetURL:URLRequest = new URLRequest(“http://yourhomepage.com/”);
    navigateToURL(targetURL,”_self”);
    }
    urlButton.addEventListener(MouseEvent.CLICK, gotoHomePage);

  7. July 4th, 2009 at 08:23 | #7

    thanks mate ! that really worked! !

  8. July 8th, 2009 at 03:15 | #8

    AHH i can’t get anything from any site to work.

    1120: Access of undefined property urlButton.

    I don’t know what i’m doing wrong, but i followed your directions to the T, however i think

    4) Select the button if it isn’t selected. Now give it an instance name of “urlButton” in the small dialog box in the bottom left hand corner

    Might be getting me messed up. I don’t see anything “instance name”, i just see Name: Type: …. then down below it has “Linkage/ checkbox for Export for actionscript, etc and then class stuff… I don’t see anything about “instance name”…

    Oh please help, i’m going nuts over this! haha

    I just don’t get it… Can’t believe that made it so dang stupid!

  9. admin
    July 9th, 2009 at 15:42 | #9

    @neosin

    OK are you using CS3 or CS4? There should be a small blank dialog box where you can enter “urlButton” on the bottom left. Looks like this: http://www.vineyardesigns.com/images/resources/flash_as/button_name.gif

    In CS4, the blank dialog box is in the top right. Do you have all the necessary windows open? If not, go to WINDOW then WORKSPACES and then click on “Default” in CS3 or “Essentials” in CS4.

    Hope that works!

  10. Vestige
    July 15th, 2009 at 01:36 | #10

    Hi all, I’m new to flash so hopefully someone can please help me out.. Using Flash CS4 with Actionscript 3.0 and it seems to me that many people are having issues with it?

    I’ve created a movie clip and added a button on the last keyframe (so that the button doesn’t appear until the end).

    I have added the following code to the last keyframe (when the button appears)

    enterbutton.addEventListener(MouseEvent.CLICK, onMouseClick);
    function onMouseClick(event:MouseEvent):void
    {
    var request:URLRequest = new URLRequest (”http://www.adobe.com”);
    navigateToURL(new URLRequest(”http://www.adobe.com”), “_blank”);
    }

    Problem is, the little hand icon appears when i hover it over the button, but nothing happens when I click.. Been trying out all different codes but nothing works and I’m getting pretty frustrated now..

    I’m sure there’s a Flash Guru who can help me out! =) Thanks in advance!

  11. admin
    July 15th, 2009 at 01:56 | #11

    I think I’d need to see the file before I could help you.

  12. Vestige
    July 15th, 2009 at 03:26 | #12

    could i please send you the file in that case?

  13. mike
    August 26th, 2009 at 11:02 | #13

    hey, I’m still new at this. how can I code this to link my buttons to Program Files on my computer, and is it any way to have the buttons put on my desktop for easy access?

  14. admin
    September 1st, 2009 at 02:12 | #14

    @Vestige
    Please send me a file

  15. admin
    September 1st, 2009 at 02:12 | #15

    @mike
    You need the location of the file on your computer. Are you using a mac or PC?

  16. Michael
    September 21st, 2009 at 21:17 | #16

    I’ve been all over the internet and nothing seems to work. I don’t know enough to figure this out on my own so hopefully someone can help. I’m using Flash CS4 with Actionscript 3. What I’ve set up is five different buttons and each of them is supposed to take you to a different part of the website; you can probably tell yourself because of the names. Everything works OK except for the fact that I can’t get these links to open up inside the same window. What am I missing?

    //SETUP

    import flash.net.URLRequest;
    import flash.events.MouseEvent;
    var GET_HOME:URLRequest = new URLRequest(“http://www.website.com”);
    var GET_INSPIRATIONAL_GALLERY:URLRequest = new URLRequest(“http://www.website.com”);
    var GET_SERVICES:URLRequest = new URLRequest(“http://www.website.com”);
    var GET_ABOUT_US:URLRequest = new URLRequest(“http://www.website.com”);
    var GET_CONTACT_INFORMATION:URLRequest = new URLRequest(“http://www.website.com”);

    //HOME

    home_btn.addEventListener(MouseEvent.CLICK,GET_HOME_ACTIVATOR);
    function GET_HOME_ACTIVATOR(event:MouseEvent):void{
    navigateToURL(GET_HOME);
    }

    //INSPIRATIONAL GALLERY

    inspirational_gallery_btn.addEventListener(MouseEvent.CLICK,GET_INSPIRATIONAL_GALLERY_ACTIVATOR);
    function GET_INSPIRATIONAL_GALLERY_ACTIVATOR(event:MouseEvent):void{
    navigateToURL(GET_INSPIRATIONAL_GALLERY);
    }

    //SERVICES

    services_btn.addEventListener(MouseEvent.CLICK,GET_SERVICES_ACTIVATOR);
    function GET_SERVICES_ACTIVATOR(event:MouseEvent):void{
    navigateToURL(GET_SERVICES);
    }

    //ABOUT US

    about_us_btn.addEventListener(MouseEvent.CLICK,GET_ABOUT_US_ACTIVATOR);
    function GET_ABOUT_US_ACTIVATOR(event:MouseEvent):void{
    navigateToURL(GET_ABOUT_US);
    }

    //CONTACT INFORMATION

    contact_information_btn.addEventListener(MouseEvent.CLICK,GET_CONTACT_INFORMATION_ACTIVATOR);
    function GET_CONTACT_INFORMATION_ACTIVATOR(event:MouseEvent):void{
    navigateToURL(GET_CONTACT_INFORMATION);
    }

  17. November 4th, 2009 at 20:09 | #17

    I spent 3 hours going site to site trying to figure this out. Other site have NO CLUE what the hell there talking about all the codes they give out FAIL over and over, and yours worked right off the bat.

    Thank you for knowing what your talking about & sharing.

    Live . Love . Laugh

    ~Reza Evol~

  18. admin
    November 5th, 2009 at 14:36 | #18

    Thanks.

  19. John
    November 19th, 2009 at 05:55 | #19

    Hello everybody!

    I have a serious problem with a code.

    This is my code:

    function makebtn(mc:MovieClip,aurl:String):void{
    mc.aurl = aurl;
    mc.buttonMode= true;
    mc.addEventListener(MouseEvent.CLICK,gotoaurl);
    }

    function gotoaurl(e:MouseEvent):void{
    var aurl:String = e.target.aurl;
    navigateToURL(new URLRequest(aurl));
    }
    makebtn(btn1,’http://www.google.com‘);
    |
    I have tried all codes to make it work for a simple button or movieclip have the task to open an URL in _blank window. This code wotks just fine, but when i put the swf in dreamweaver and publish in html, the code for the just simple doesn´t work!

    The button and code is split into two windows (content), 1, Scene 1 -> ScrollContent -> content.

    The buttons placed outside the ScrollContent with the same codes work. But the others in content area (wich have the scroll) doesn´t works!
    Can anyone help me? please!

  20. donv
    November 24th, 2009 at 23:57 | #20

    This is the best site I have ever found. Thank you for providing straight forward answers and info.

  21. Mikael
    February 23rd, 2010 at 20:34 | #21

    Great tutorial. This is the only one I found that makes sense!

  22. April 19th, 2010 at 21:31 | #22

    THANK YOU!!!!!!!!! You have no idea how desperate I was to find this help. THANK YOU!

  23. evil-disco
    June 29th, 2010 at 03:03 | #23

    thank you it works

  24. jodie
    July 1st, 2010 at 13:58 | #24

    Thank you so much for your help, I searched for ages for some code like this, and it works perfectly.

  25. admin
    September 3rd, 2010 at 09:35 | #25

    BTW, are there any other flash tutorials you would like to see?

  26. September 22nd, 2010 at 15:38 | #26

    Very good, clear, logical tutorial, the best I found online! THX

  27. October 25th, 2010 at 20:31 | #27

    Thank you so much !!!

  28. ken
    October 26th, 2010 at 09:39 | #28

    i have a problem with a button in flash cs4.Its working in flash but not on the browser

  29. Juggelnuat
    November 3rd, 2010 at 09:57 | #29

    Hi,

    I’m having the same problem as Vestige. I have an animation that I’ve created where the button comes in at the end.
    When I put the code in the animation doen’t play at all. It doesn’t give me any compiler errors it just says the following under output:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Banner01_fla::MainTimeline/frame1()

    If I put the button in frame 1 everything works perfectly. However as soon as I move it to another frame (ie. later in the animation) it stops working.
    Do I need to the EventListener to point somehow to the specific frame where the button comes in??
    How do I code that??

  30. JP_2011
    June 27th, 2011 at 15:01 | #30

    Is flash that ridiculous where people have to search high and low for a tutorial on how to create a simple link? The program should be canned and filed away in a government warehouse somewhere never to be seen again

  31. Vrenn
    July 18th, 2011 at 18:50 | #31

    Just as a suggestion for those of you using the code above.
    My past experiences always told me that when you copy and paste the code directly into the action script panel, you may get syntax errors when publishing. Do not do this. Sometimes the pasting from the site will add an improper space or break the code in general.
    Best is to type the code out exactly with the proper beginning and end brackets. Test it out and see if it works. Hope this helps.

  32. admin
    July 22nd, 2011 at 17:52 | #32

    I sense a little frustration here. The problem with computers and programming languages is they are created by people who know how to use them. Then there’s the rest of us…

  33. August 26th, 2011 at 08:39 | #33

    hi..gr8 tutorials! thanks :) i am developing a video player similar to YouTube. i need to add EMBED option in that so that my video can b shared on other sites like Facebook. (please refer to you tube Share/Embed option if i am not being clear here). On clicking Embed Option(button) a pop up box should appear which will give the HTML code. that link if pasted elsewhere should add that video in that page n play there(similar to YouTube Embed features) . Please let me know.

  34. August 26th, 2011 at 08:41 | #34

    the flash video player is built using Flash CS3 ,Actionscript 2.0.

  35. September 16th, 2011 at 08:05 | #35

    how to open a popup in same stage. On stage has button, when click the button then open the popup. Is it make Flash Action Script 3.0

  36. September 20th, 2011 at 14:13 | #36

    How to open a popup in same stage. On stage has button, when click the button then open the popup. Is it make Flash Action Script 3.0

  37. Raghu
    September 27th, 2011 at 05:07 | #37

    Hi i have designed the home page i need to give link for next button like AboutUs this script is not working canu please help me out thank you

  38. aizat
    October 7th, 2011 at 21:50 | #38

    thx mate..its really work for me..

  39. admin
    October 8th, 2011 at 00:07 | #39

    Great, glad to hear it. Thanks for checking out the site.

  40. James
    November 22nd, 2011 at 04:31 | #40

    Cheers for a CLEAR & SIMPLE tutorial on creating a link from a button. Much respect, appreciate it!

  41. Alex
    November 22nd, 2011 at 14:32 | #41

    Glad you liked it. Thanks for checking out the site.

  42. PePe
    December 8th, 2011 at 14:39 | #42

    Hi!

    Thanks for the code. My only problem is that there’s an AS3 glowing effect in the code as well, and if it is running, it gets to the top of the area (the link do not works until it is over).

    Have you got any ideas how to solve it?

    Thanks again!

  43. Tom
    December 20th, 2011 at 23:52 | #43

    If you get the error “Syntax error: expecting rightparen before rightbrace” it’s because you copied and pasted the code.

    The error for me was fixed after i deleted the both the ( and “ symbols on the 3rd line in URLRequest(“http://yourhomepage.com/“); i.e. the opening bracket and quotation mark.

    Entering them in manually worked a treat. FYI i’m using Firefox to copy the code.

    Thanks for the code Alex – this guide is simple and to the point. Top marks for you sir.

    As a side note/rant; AS3 is awful, I can’t wait for Flash to be scrapped completely, Adobe only have themselves and their awfully complex programs to blame…It shouldn’t be this hard to create a hyperlink!!

  1. No trackbacks yet.