ActionScript 3 Tutorial – What Happened to getURL?

Author : Craig

Author's Website | Articles from

For those of you who are new to ActionScript 3, you might be wondering what happened to the getURL() function. It used to be so easy to create outbound links in Flash with just a single line of code inside the onRelease event for your buttons. Which brings up another question...what happened to the onRelease event?

If you wanted to create a link to another website in ActionScript 2, here's what it would have looked like:

code

Ah, if only it were still so simple!

First of all, the way we handle events in AS3 is a little different. We're still attaching functions to event handlers, but we're doing it in a little bit of a different fashion. First we have to explicitly add an event listener to our button, and within that event listener, we tell Flash which function we want to respond to that event. Then we create that function separately. Like so:

code

Notice that inside the parentheses for the "addEventListener" function, the first thing we specify is the type of event we're listening for. In this case, we're listening for the CLICK event, which is defined in the MouseEvent class. The second thing we've specified for our event listener is the name of the function we want to run when our event listener is triggered. You can give this function any name you please, as long as the spelling and capitalization matches up with the actual name that you give the function. In this example, I've called the function onMouseClick. It's inside this onMouseClick function that we want to insert our code for navigating to an external URL.

Well, for those of you who are used to using getURL to take you to another website, it's time to introduce you to a new little method called navigateToURL. Sure, it's a little more verbose, but it seems to get the point across better.

But before you go out there and starting replacing all your getURL methods with navigateToURL, let me warn you about something--it won't work. At least not the way you're used to. When you call on the navigateToURL(), you might be tempted to type a URL inside the parentheses. But the navigateToURL method isn't looking for a URL string. Instead, it's looking for a URLRequest object. The URLRequest class is what allows us to communicate with other websites and external files. So, here's what your final code would look like:

code

In line 5, we're creating a new URLRequest object, in which we're storing the String containing our URL. We stored this URLRequest object in a variable named request. Then, in line 6, we call on the navigateToURL method, which points to the request object we just created.

Note: The "_blank" is optional. Include this only if you want the link to open in a new browser window.

I know this has been a long explanation for such a simple concept, but I've seen a lot of very brief explanations out there, and I wanted to make sure you understood not only what you were typing but also why you were typing it.

Like this post? Share it!

  • Tweet
  • Facebook
  • Diggit
  • Delicious
  • Diggit
  • Diggit
  • Diggit
  • Diggit
  • Diggit

Related Posts


User Comments


  1. aman
    May 17, 2009

    how do i link the page within the website
    e.g: i want the home button to be linked to the home.aspx page in the same folder
    home_btn.addEventListener(MouseEvent.CLICK, homeClicked);
    function homeClicked(event:MouseEvent):void
    {
    /*???*/
    }

    Reply

    • niyi
      September 13, 2011

      i have also been having problem with the flash cs3 , but i i have found a solution to the problem of linking to a page.

      (AS3)
      instance-name-goes-here.addEventListener(MouseEvent.MOUSE_UP, link);

      function link(event:MouseEvent):void
      {
      var R:String = “name.html”;
      var G = new URLRequest(R);
      navigateToURL(G,”_blank”);

      }

      Reply

  2. Claire
    May 29, 2009

    I am having the same trouble as Travis. Did you get this to work for you once the movie was published? I can’t figure it out.

    My movie works while testing but not after it’s published. Anyone have any ideas?

    Reply

  3. Claire
    May 29, 2009

    This is to help out someone who also got stuck here: Flash movie links work during Test Movie but not after publication. I had to set the Global Security Settings panel (search for it online) to accept the folder on my PC. After I did that, closed and reopened the application, republished the Flash movie, all the links worked.

    Reply

  4. Chammy
    May 31, 2009

    Is there anyone who can tell me what exactly seems to be the problem with my script… keeps giving me an error ‘{‘ expected…??
    btn1.addEventListener(MouseEvent.CLICK, onMouseClick);

    function onMouseClick(e:MouseEvent):void
    {
    var request:URLRequest = new URLRequest(“https://www.russellcampbelracing.co.za”);
    navigateToURL(request,”_blank”);

    }

    Reply

    • trenoops
      June 17, 2011

      Do you have an external .as file associated with your SWF file (i.e. Main.as)? If so then you may have to delete the :void statement after your function. OR add the script to your .as file and set your function to ‘private function’

      Reply

  5. hoddie_ninja
    June 1, 2009

    Thanks for the tutorial. Your explanation was very good. thank you.

    Reply

  6. Agris
    July 7, 2009

    Place this code in any frame at your movie clip timeline:

    navigateToURL (new URLRequest(“http://www.domain.com” ), “_self”);

    –> For making flash go to your URL when movie clip ends

    —————————————————

    Place this code in any frame at your movie clip timeline:

    button.addEventListener(MouseEvent.CLICK, onMouseClick);
    function onMouseClick(e:MouseEvent):void
    {
    var request:URLRequest = new URLRequest (“http://www.aterno.lv” );
    navigateToURL(new URLRequest(“http://www.aterno.lv”), “_blank”);
    }

    –> For making your button (do not forget to name it “button”) go to your URL

    Reply

  7. Vestige
    July 14, 2009

    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!

    Reply

  8. Dan
    July 21, 2009

    Hey man, awesome website! I have more of a programming background and was just picking up on Actionscript 2 when I decided to dive into Actionscript 3. I’m glad I stumbled across your website as it’s answered a lot of my questions in a real short time! Thanks!

    Reply

  9. derek
    July 29, 2009

    someone should post a fla with two functional links then we could all just reuse something that works as adobe couldn;t even offer us that much. Adobe messed up with this so that their program wont reach as many developers as it could have, once I find my cut and paste thats all im ever gonna do. there is no reason to understand the nonsense so long as its grappled to the point of functional manipulation

    Reply

  10. SunWuKong
    August 12, 2009

    Hi and thanks Craig, found this very easy to follow and intuitive. Why? Because I already program in Java, and this is exactly how I expect this to be done. Sorry to everyone who finds this method long winded and difficult, but if you need a reason for your torment, then here it is. Basically a large subset of Flash Developers are Java programmers building Rich Internet Applications (RIAs) and this change caters to their need to have a flash scripting language that does not differ drastically from Java’s. Different for them means more errors, longer and more expensive develolment time. Adobe is just going where the money is, obviously.

    Reply

  11. Joey
    August 24, 2009

    Hi, Thank you so much!Really appreciate when someone takes the time to go and put up a really useful tutorial. Keep up the good work!

    Reply

  12. Jason
    August 30, 2009

    Just wanted to say a quick “Thanks!” for such a straight forward tutorial about a very vital aspect of web design. I could have saved hours of my life had I just come here first instead of browsing all the ridiculously specific tutorials elsewhere. Thanks for helping my on my way to building flash sites!

    Reply

  13. BDTR
    September 24, 2009

    This gave me exactly what I needed. Thanks!

    Reply

  14. Farah
    September 28, 2009

    Wow, thx so much, this code is “fantastic” hauhau

    thx again!

    Reply

  15. Carvalho
    November 18, 2009

    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!

    Reply

  16. satya
    January 29, 2010

    HI i m satya.
    I really hate AS3….

    Please help me out for this..

    In swf at last frame I want to open a url without any click
    but right now its not working with following code.

    —- as3 code ——

    stop();
    var request:URLRequest=new URLRequest(“http://www.villaisla.co.in/Villaisla.html”);
    navigateToURL(request,”_self”);

    PLease help me….

    Reply

  17. Salaka
    February 12, 2010

    Dears, i went throught all the conversation up so is it mean we can’t add more then geturl() link with the AS3 is there any other way to include more then one.

    Reply

  18. Salaka
    February 12, 2010

    Thanks for You All, No Need to Reply i fix it as below and it’s work fine:

    a.addEventListener(MouseEvent.CLICK, onMouseClick);
    function onMouseClick(e:MouseEvent):void
    {var request:URLRequest=new URLRequest(“discover_course.htm”);
    navigateToURL(request,”_blank”);
    }
    b.addEventListener(MouseEvent.CLICK, onMouseClick01);
    function onMouseClick01(e:MouseEvent):void
    {var request:URLRequest=new URLRequest(“open_course.htm”);
    navigateToURL(request,”_blank”);
    }
    c.addEventListener(MouseEvent.CLICK, onMouseClick02);
    function onMouseClick02(e:MouseEvent):void
    {var request:URLRequest=new URLRequest(“advanc_course.htm”);
    navigateToURL(request,”_blank”);
    }
    d.addEventListener(MouseEvent.CLICK, onMouseClick03);
    function onMouseClick03(e:MouseEvent):void
    {var request:URLRequest=new URLRequest(“efr_course.htm”);
    navigateToURL(request,”_blank”);
    }
    e.addEventListener(MouseEvent.CLICK, onMouseClick04);
    function onMouseClick04(e:MouseEvent):void
    {var request:URLRequest=new URLRequest(“rescu_course.htm”);
    navigateToURL(request,”_blank”);
    }
    f.addEventListener(MouseEvent.CLICK, onMouseClick05);
    function onMouseClick05(e:MouseEvent):void
    {var request:URLRequest=new URLRequest(“master_scuba_course.htm”);
    navigateToURL(request,”_blank”);
    }

    Reply

  19. Exotic
    February 20, 2010

    Thx a lot :)

    Reply

  20. Robert Hull
    February 20, 2010

    Hello I am getting back into flash, I used to be pretty good at flash mx, but now with cs3 and action script 3.0 I cannot add a get url at the end of a timeline to work. Here is what I am doing:

    I have greenscreened a video and imported it into flash cs3 with action script 3.0 for it to work, with a background and by looking at the code you have done, it should work but I get errors and my green screen video disappears? Any ideas?

    Reply

  21. Kevin O'Brien
    February 25, 2010

    I just wanted to say THANK YOU for this post, it was extremely helpful. THX

    Reply

  22. Nuthman
    March 3, 2010

    I created a little getURL class for actionscript 3 that simulates as2 getURL here: http://www.as3blog.org/?p=40

    Reply

  23. Sam
    April 1, 2010

    Hi,

    I have little experience with AS3 – I have followed what you said above but I get the following error and the links won’t work ….

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

    I have found lots of stuff online but no definitive solution …. any one any ideas. The error is only thrown when I put the above code into my actions keyframe, when i take this code out it is fine and the other buttons work as they should.

    Any ideas?

    thanks in advance ….

    Reply

  24. Scottaburger
    April 16, 2010

    Alexandre de Oliveira, you’re the best for addressing the “1120: Access of undefined property … ” problem.

    For people with this problem see post #92.

    Reply

  25. 3TurtleStudios
    April 16, 2010

    Actionscript 3.0 code is definitely a lot more sensible and organized than its predecessor… Although it does longer to write codes in some cases, its easier to learn…

    Reply

  26. David
    July 21, 2010

    Seriously… thank you, I was getting really irritated. You solved my problem.

    Reply

  27. Phil
    May 31, 2011

    Thank you so much.

    As a side point, why is it that everytime I need to use flash I seem to have to relearn how to use it ? Think I might just learn HTML5 and say goodbye to flash for ever…

    Reply

  28. Jason
    June 23, 2011

    Thanks!
    Just what I needed. I get confused switching from AS2 to AS3 simple things link linking a URL seem to have changed.

    Reply

  29. Emily
    August 16, 2011

    Is it possible to run the url, but never actually open a window of any kind? I’m trying to run a URL to increment a poll in a database and I don’t actually want to open a page in a browser…

    Thanks!

    Reply

Leave a Reply

 
  Twitter Followers

Stock Flash Files Web Development Tutorials

Stock Graphics Video Files


Your Ad Here