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:
<img src="http://img.skitch.com/20080418-p896t54u6iy31w8ctsqxn7ee7h.jpg" border="1" alt="code" width="368" height="115" />
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, Flash, getURL, navigateToURL, URLRequest
i do not know much but i think you need to eliminate the first “this.” ,,,,, do not take my word but i will look into the CraigTutorials for it
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
{
/*???*/
}
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”);
}
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?
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.
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”);
}
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’
Thanks for the tutorial. Your explanation was very good. thank you.
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
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!
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!
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
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.
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!
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!
This gave me exactly what I needed. Thanks!
Wow, thx so much, this code is “fantastic” hauhau
thx again!
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!
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….
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.
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”);
}
Thx a lot
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?
I just wanted to say THANK YOU for this post, it was extremely helpful. THX
I created a little getURL class for actionscript 3 that simulates as2 getURL here: http://www.as3blog.org/?p=40
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 ….
Alexandre de Oliveira, you’re the best for addressing the “1120: Access of undefined property … ” problem.
For people with this problem see post #92.
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…
Seriously… thank you, I was getting really irritated. You solved my problem.
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…
Thanks!
Just what I needed. I get confused switching from AS2 to AS3 simple things link linking a URL seem to have changed.
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!
we are trying to use the file:/// syntax to link to a local swf file for a flash dvd. How does one specify the relative drive paths so that it can run on another pc?
BRO
I Love U
thx a lot of