ActionScript 3 Tutorial - What Happened to getURL?
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:
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:

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:

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.
Tags: ActionScript 3 Tutorial, addEventHandler, event handlers, Flash CS3 tutorial, getURL, navigateToURL, URLRequest
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.





April 20th, 2008 at 8:22 am
Hello. Thanks for the tutorial but why did you put the script into an image file instead of giving us the plain text to copy and paste?
April 20th, 2008 at 2:06 pm
Actually, I DO have a good reason for it. If you type the code in yourself, it will stick in your memory better than if you just copy and paste it. Basically, I’m forcing you to practice! Does that make sense?
~Craig
April 24th, 2008 at 6:01 pm
What if you just want it to go to a url when the movie is over?
April 25th, 2008 at 5:04 am
Very Interesting!!!
April 25th, 2008 at 5:42 am
[…] navigateToURL. You might be asking why would the amount of lines increase for such a simple task. Craig Campbell just recently posted an in depth example and explanation to creating outbound links within […]
April 30th, 2008 at 4:53 am
Ahhh I can’t believe Adobe took something so easy and just ruined it!
April 30th, 2008 at 7:21 am
it doese no work with me ..i have always a security error that i can not enter this web site..can i have some help please.. thanx
(SecurityError: Error #2028: Le fichier SWF local-système de fichiers file:/)..
May 4th, 2008 at 6:16 am
Hi all, I’m new to flash (but not new to programming) and I have a straight remark : how on earth is it possible that the most useful thing like clicking on a banner to be forwarded to a website is so complex in AS3? It should be a simple option of the movie in the publish settings. I simply want any click on my banner to forward to my website, I don’t want anything fancy of any specific text or button to do it, I want the entire banner to do it, and I’m now forced to create a hack with a large transparent button and “uncompilable” code… Pffffff… anyway, thanks for your help
May 4th, 2008 at 11:25 am
Craig, freand! Dear friend! It works!!!! Just unbolievebel!!!!!!!!!!!!!!!!!!
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(”http://www.prodesign.890m.com/index.html”);
navigateToURL(request,”_blank”);
}
May 11th, 2008 at 10:51 am
I have the following code published in html with an embed F9 AS3 swf file:
———————————————————–
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest=new URLRequest(”http://www.in.gr”);
navigateToURL(request,”_blank”);
}
———————————————————–
When i click the button it doesn’t go to the link.
When i open the swf with flash player it does? why does this happen?
May 11th, 2008 at 8:04 pm
hi there! i am using several buttons to link to different sites. i was unsuccessful with the above code, but i think i found code to put on my buttons that will work:
navigateToURL(new URLRequest(”http://www.ccsf.edu”), “_blank”);
is this because I tried to declare the same variable twice? sorry, i’m new at programming.
May 13th, 2008 at 3:05 pm
ok but where do you put this script, if not on an object like in AS2
May 13th, 2008 at 3:37 pm
@dan: You add the script to a keyframe. I usually keep my actionscript in its own separate layer called “Actions.” Click on the keyframe in the Actions layer, open up your Actions Panel, and enter the code.
May 14th, 2008 at 8:16 am
I tryed that on a separeted layer but than for some reason
my txeff plugin (http://www.txeff.com/) action script or movie for some reson want work.. I thank you for your time, just this if you know something about
May 22nd, 2008 at 5:46 am
I have a full flash web template with 5 pages. Within those pages I have buttons and hot spots I want to link to pdf files and new pages associated with the content of that page–a kind of “More” button. The tutorials I find instruct for adding pages to the main timeline or as above linking to external URLs. Is there a script for linking within the fla files? By the way, I have bought two books and neither have ActionScript for what I am trying to do. I am working in CS3.
May 28th, 2008 at 7:08 pm
You can condense lines 5 and 6:
function onClickHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(”http://www.whatever.com”));
}
May 31st, 2008 at 9:52 pm
It worked, but keeps opening a new browser window even though I exclude the “_blank” ???
June 12th, 2008 at 1:27 am
I copied the code as explained:
button.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(”http://www.msn.com”);
navigateToURL(request, “_blank”);
}
even used the condensed version as “Worked” posted. Both does not work. It keeps stating 1 error “Description: ‘{’ expected; Source: function onMouseClick(e:MousEvent):void”
I tried placing this AS in it’s own layer, with the button, inside the button keyframe, etc. and none launched me to MSN.com. Please HELP. What the heck am I doing wrong?
June 20th, 2008 at 10:11 am
because the onMouseClick action is repeated so u have to rename it for example if in your first button you have this
mybutton_mc.addEventListener(MouseEvent.click,onMouseClick);
Function onMouseClick(e:MouseEvent):void
your second button must have the onMouseClick like this
mybutton_mc.addEventListener(MouseEvent.click,onMouseClick2);
Function onMouseClick2(e:MouseEvent):void
and so on, as far i understand AS3 does not allow repeated actions so thats why you have to find a way easy for you to work with this, hope this helps
June 24th, 2008 at 1:09 pm
Thank you for this…the “how” worked very well.
I still don’t understand why they did this and why it’s better to do it this way. Does it offer more flexibility for varying options? Does it allow for better browser integration? Please explain.
June 29th, 2008 at 5:51 am
I have the same problem as Vasser. I get a security warning about the swf trying to do something unsafe and a message about changing security settings, then it closes.
This is on Windows Vista.
Any way to get around this?
July 7th, 2008 at 9:59 am
My problem is a little different.
This thread the closest thing that I’ve found to a solution on the web so far…I’m hoping I can find help here.
I am creading a CD Rom website with Flash CS3 and I need my flash button to link to a PDF file, opening it in a new browser window.
I used the script above and replaced the URL address with the path to my pdf file ( //content/1100w.pdf ) to no avail.
Craig! Help me out please! I’ve lookied through all of the LeranFlash.com tutorials that I have and could’nt find the solution.
Any help would be greatly appreciated.
July 12th, 2008 at 2:16 pm
Mike,
Try your code without the extra navigation information. Just use “1100.pdf” in the request and make sure the file is in the same directory as your swf or exe file.
Keith
July 12th, 2008 at 11:09 pm
I am trying to do a slide sync in AS3.0 and have everything in order until I tried to add a button to refer to a URL. Please take a look at my code and I added the suggested navigate to a URL code at the end of my other scripts. When I do this my flv Component icon just flashes and the movie no longer plays and I can not access the intended URL either. I started off with this template and just hoped to be able to use the get url command from AS2.0 associated with a specific button and that no longer seems possible… what a bummer
Any suggestions would be most appreciated.
// *************************
// Stop the timeline
stop();
// *************************
import fl.video.*;
import flash.events.*;
// Button hilite graphic
var highlight_mc;
// *************************
// Navigation buttons call this function to trigger a change
// in the video and the view of the content
function seekToCuePoint( cueName ) : void
{
// Find the cue point in the FLVPlayback component, seek
// to time, and highlight the next button for a quick redraw
var c = display.findCuePoint( cueName );
display.seekSeconds( c.time );
findNextButton( c.name );
}
// *************************
// The natural flow through the video or jumping by seeking
// forward and backward will trigger the following function
function synchVideoToInterace( cueName:String ):void
{
// Play if needed
if(!display.isRTMP && !display.playing ){
display.play();
}
// Show associated content on screens or labeled frames
gotoAndStop( cueName );
// Update the higlight position
findNextButton( cueName );
}
// Layout the button highlight when requested
function findNextButton( cueName:String ):void
{
// Look for buttons named with the current cue point
// name plus the letters ‘_btn’ (i.e. studiomx_btn)
if( getChildByName(cueName+”_btn”) != null )
{
var cueBtn = this[cueName+”_btn”];
// Attach the highlight clip if needed
if( this[”highlight_mc”] == undefined ){
highlight_mc = new ThumbOutlineSelected();
addChild(highlight_mc);
}
highlight_mc.x = cueBtn.x;
highlight_mc.y = cueBtn.y;
}
}
// *************************
// Create event handler functions to catch events from
// the video component and update the interface…
function cuePointHandler( evt:MetadataEvent ):void
{
// Get the cue name from the event object
synchVideoToInterace( evt.info.name );
}
display.addEventListener(MetadataEvent.CUE_POINT,cuePointHandler);
// *************************
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest=new URLRequest(?http://www.smpwebvideo.com?);
navigateToURL(request,?_blank?);
}
July 14th, 2008 at 6:37 pm
Craig,
This navigateToURL code works great when I test the flash movie, but when I publish it to the web site and click on the button, it doesn’t work. What’s missing?
I get this error message in Flash: WARNING: Actions on button or MovieClip instances are not supported in ActionScript 3.0. All scripts on object instances will be ignored.
July 14th, 2008 at 8:31 pm
I’m fairly new to all programing and I’ve been learning from different posts online and with messing around. However I have not found a way to link to internal html files I have stored on a server. This is what i have
HOMEBUT.addEventListener(MouseEvent.CLICK,onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest=new URLRequest(”/index.em?pid=817786″);
navigateToURL(request);
}
Is there a different way for relative urls and is that even the right term for them? Any answer would be appreciated.
July 18th, 2008 at 11:45 am
Still can’t figure this out. I created a layer called “Actions” Now I’m supposed to create a keyframe, but where? In Up, Over, Down or Hit?. I tried it in Hit, but when I open the Actions panel, it says “Current selection cannot have actions applied to it”
July 19th, 2008 at 6:24 am
Sneed: The actions need to be placed in the timeline where the button is placed, not in the button’s timeline. For example, if you placed your button on the main stage, you need give your button an instance name, then create a new layer on the main stage’s timeline for your ActionScript. If your button is in frame 1 of the main timeline, then you don’t need to add an extra keyframe, because there’s already a keyframe in frame 1 when you create the layer.
Hope this helps!
Craig
July 22nd, 2008 at 1:02 am
There was an earlier post that pretty well framed my question - What if you just want it to go to a url when the movie is over? I don’t need a button, just want to have the user brought over to a new web page once the flash movie has finished. I’m very new to Flash and would appreciate any help on this. How is that done?