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.
















DiegoDx
can you just this?
import flash.net.*;
navigateToURL(new URLRequest(’http://www.google.com’),”_self”);
IT WORKS?
Xak
Anyone who is having problems with the code they “cut and pasted” from this page, replace the “” signs in the code and everything should work again
Alessandro Flasher
Thanks for the great piece of science coz all U wrote was helpful to me.
By the way, do you remember the problem with just plain
on(release){
getURL(“/blablablablabla/bla.html”, “_self”)
}
in AS2.0 when it was working good for AS1.0? I’m still wondering was it because of 2.0/1.0 difference? Or it was any other reason?
nikzouly
hey all
i’m using actionscript 3.0
please i need the exact code to link two swf file that are located in the same folder.
not to link the button into a website that already in the web
goingmad
Hi there. I have designed a banner and have so far spent the best part of a day trying to get one button to link to a URL. The button appears at the end. Although the info above is clear and as far as I can see I have followed the info but it keeps coming up with the SAME error.
**Error* Scene 1, layer ‘actions’, Frame 65
line 1: 1120: Access of undefined property my_button.
my_button.addEventListener
(MouseEvent.CLICK, onmouseclick)
My code looks like this:
my_button.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://rfs.seafish.org/”);
navigateToURL(request, “_blank”);
}
Also I want my banner not to loop but the stop(); ceases to work when i add in the link code.
HELP!!!
Thanks in advance!
sophia
hey, i tried to codes and various other codes i got from this side but keep getting the error message saying
“The class or interface ‘MouseEvent’ could not be loaded”
What could possibly be wrong?
raymond
In your button properties–give your button an unique instance such as myButton without _ in it and try the code below. And remember to add your action codes in it’s own layer. Example: Layer1–actions
Layer2–background
Layer3–buttons
Layer4–images
blah,blah,blah–u get the idea.
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://www.yoursite.com”);
navigateToURL(request,”_blank”);
}
raymond
The above code was for “goingmad”.
goingmad
Raymond – Thats for your suggestions and code. The actions layer is separate and I re-named the button myButton, as well as copying and pasting in your code but unfortunately I am still getting an error message. This time it says:
**Error* Scene 1, layer ‘actions’, Frame 65
Line 5: 1093: Syntax error
navigateToURL(request,”_blank”);
Cheers again
raymond
goinmad–
Here i’ve created three basic square buttons giving them names as follows: The action is placed in one of button layer and not an action layer–of course this is for example purpose and it works as well. But i would recommend putting the action in it’s own layer–simplicity thats all..
Button1 label–my_button; Instance name–my_button
Button2 label–my_button02; Instance name–my_button02
Button3 label–my_button03; Instance name–my_button03
Here is my code in action action window:
my_button.addEventListener(MouseEvent.CLICK,clickHandler);
function clickHandler(event:MouseEvent):void{
navigateToURL(new URLRequest(“http://www.adobe.com”));
}
my_button02.addEventListener(MouseEvent.CLICK,clickHandler02);
function clickHandler02(event:MouseEvent):void{
navigateToURL(new URLRequest(“http://www.msn.com”));
}
my_button03.addEventListener(MouseEvent.CLICK,clickHandler03);
function clickHandler03(event:MouseEvent):void{
navigateToURL(new URLRequest(“http://www.yahoo.com”));
}
Each button link to assigned it’s own URL. Remember to assign a different clickHandler for each button as i did with mine.
Hope it helps.
Ross
Hi,
I’d really appreciate a bit of guidance here if anyone can spare the brainpower. I’ve used various methods and codes to get a animated button with a link on and this seems great.
The only problem I am encountering is getting my links to reload within the same window, what I had previously thought of as “_self”. If I put “_blank” or inothin at all t works just fine and pops open a new browser window but what I need is for it to open the link within the window the .swf is actually embedded in, not a new one.
Any thoughts or pointers would be really great and I can see a few people having had similar problems on this thread.
Thanks in advance,
Ross
Rachael
Hi I wonder if anyone can help ?
I am trying to get an external swf to open in a new window from a button on my main swf.
Currently i have which works but lays on top of the main swf.:
imageGallery.addEventListener(MouseEvent.CLICK,magPicture);
function magPicture(e:MouseEvent):void {
var imageRequest:URLRequest= URLRequest(“magPicture.swf”);
var imageLoader:Loader=new Loader();
imageLoader.load(imageRequest);
addChild(imageLoader);
}
It would be a really good help if someone could advise as it is for an assignment for uni and i am running out of time.
Thanks
Rudolf
Hi, thanx for a good indepth tutorial.
I only experience the same problem as Ross and Melanie: I excluded the “,_blank” yet the URL still opens in a new window. How do you get it to open the URL on the same window?
Thanx.
R
Jon
Hi, Just wanted to say thanks to raymond for a good save on the instance name thing
‘In your button properties–give your button an unique instance such as myButton without _ in it and try the code below. And remember to add your action codes in it’s own layer.’
spot on all works well now!
srawr42
Hello,
I am attempting to make a clickable image map which would link to a series of other pages.
I tried to apply your code to what I have but it doesn’t seem to be working.
This is what I have:
westernsahara.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick (e: MouseEvent):void{ var request:URLRequest = new URLRequest (“http://www.international.ucla.edu/africa/countries/article.asp?parentid=96696″); navigateToURL(request, “_blank”); }
And this is the error I keep getting:
1120: Access of undefined property westernsahara.
It’d be much appreciated if you could help…thanks!
Mike
Hey,
i have my whole entire code in place and a OUTPUT error comes up. Here’s what it says:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at riders2_fla::MainTimeline/frame144()
does anyone know what that means? or what i’m suppose to do?
here’s my code:
stop();
frank.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://doubledubco.tripod.com/frankkreyssig.html”);
navigateToURL(request,”_blank”);
}
andrew.addEventListener(MouseEvent.CLICK, onMouseClick2);
function onMouseClick2(e:MouseEvent):void
{
var request: URLRequest = new URLRequest(“http://doubledubco.tripod.com/andrewfowler.html”);
navigateToURL(request, “_blank”);
}
bobbyp.addEventListener(MouseEvent.CLICK, onMouseClick3);
function onMouseClick3(e:MouseEvent):void
{
var request: URLRequest = new URLRequest(“http://doubledubco.tripod.com/bobbyproctor.html”);
navigateToURL(request, “_blank”);
}
cory.addEventListener(MouseEvent.CLICK, onMouseClick4);
function onMouseClick4(e:MouseEvent):void
{
var request: URLRequest = new URLRequest(“http://doubledubco.tripod.com/corychampagne.html”);
navigateToURL(request, “_blank”);
}
frankie.addEventListener(MouseEvent.CLICK, onMouseClick5);
function onMouseClick5(e:MouseEvent):void
{
var request: URLRequest = new URLRequest(“http://doubledubco.tripod.com/frankieglawson.html”);
navigateToURL(request, “_blank”);
}
bj.addEventListener(MouseEvent.CLICK, onMouseClick6);
function onMouseClick6(e:MouseEvent):void
{
var request: URLRequest = new URLRequest(“http://doubledubco.tripod.com/bj.html”);
navigateToURL(request, “_blank”);
}
blackattack.addEventListener(MouseEvent.CLICK, onMouseClick7);
function onMouseClick7(e:MouseEvent):void
{
var request: URLRequest = new URLRequest(“http://doubledubco.tripod.com/bigblack.html”);
navigateToURL(request, “_blank”);
}
zach.addEventListener(MouseEvent.CLICK, onMouseClick8);
function onMouseClick8(e:MouseEvent):void
{
var request: URLRequest = new URLRequest(“http://doubledubco.tripod.com/zach.html”);
navigateToURL(request, “_blank”);
}
matt.addEventListener(MouseEvent.CLICK, onMouseClick9);
function onMouseClick9(e:MouseEvent):void
{
var request: URLRequest = new URLRequest(“http://doubledubco.tripod.com/mattbrown.html”);
navigateToURL(request, “_blank”);
}
some please help me, i really need to know why it has an error
JEz
This is exactly what I am looking for, but want to have multiple buttons with URL links, how can I do this I have tried this:
myButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://www.mmkiteboarding.com”)
navigateToURL(request, “_blank”);
}
+ copy and pasted it changing the “myButton” to “myButton_1″ etc etc but no luck could someone please help thanks
J
Benny
If the video is just gonna go to an URL when the movie is over, just place this code at the last frame:
stop();
var request:URLRequest = new URLRequest(“http://yoururl.com”);
navigateToURL(request,”_self”);
Brian
Hey uh.. i used this code and it’s like perfect but it stops the animation (timeline). How to do i continue the timeline?
Here’s wat the code looks like on a separate layer i created for the actionscript on the first possible frame.
banner01.addEventListener(MouseEvent.CLICK, onMouseClick1);
banner02.addEventListener(MouseEvent.CLICK, onMouseClick2);
function onMouseClick1(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://www.yahoo.com”);
navigateToURL(request,”_blank”);
}
function onMouseClick2(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://www.google.com/”);
navigateToURL(request,”_blank”);
}
So… how do I continue playing the animations.
Robert
Being able to cut and paste the code would be a nice addition to the page.
Thank
you for the useful information.
wallpaperama
i just started learning AS3 and from the looks of it, AS2 was better, why change it. i guess there must be something im not seeing. but i appreciate your time and effort
mike
im trying to make multiple links on one page and in flash it goes to both, but on the web it only goes to one….can someone help me?
here’s the code:
adminsbb.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://www.myspace.com/niztech”);
navigateToURL(request,”_blank”);
}
mikekern.addEventListener(MouseEvent.CLICK, onMouseClick2);
k
function onMouseClick2(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://www.myspace.com/mikekern”);
navigateToURL(request,”_blank”);
}
Hector
First, I hate you ADOBE. Really, I do.
I think I can answer everyones questions with my discovery, here we go:
//Top of page
var myFirstLink:URLRequest = new URLRequest(“http://mywebsitelink.com”);
//Button Action
buttonOne.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(MouseEvent):void {
navigateToURL(myFirstLink, “_self”);
}
Things you need to know: The action and the button must be on different layers. For those of you who want more than one button on the page with different links, the code would look like this:
//Top of page
var myFirstLink:URLRequest = new URLRequest(“http://myfirstwebsitelink.com”);
var mySecondLink:URLRequest = new URLRequest(“http://mysecondwebsitelink.com”);
// Button Action
buttonOne.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(MouseEvent):void {
navigateToURL(myFirstLink, “_self”);
}
buttonTwo.addEventListener(MouseEvent.CLICK, onMouseClick2);
function onMouseClick2(MouseEvent):void {
navigateToURL(mySecondLink, “_self”);
}
Things you need to know: All buttons MUST BE on the same frame number. Otherwise, only the first button on the timeline will work; you’ll just click on the other buttons and nothing will happen. If you need more buttons just C&P the code and be sure to change the “var myFirstLink…” to a different name and the reference it at the “navigateToURL…” line.
PLEASE READ: Check all the quote marks”". This site seems to change them. I spent 10 minutes trying to figure out why I was getting an error about my quote marks.
Hope this helps.
/RANT
For those of you (Craig Campbell) creating tutorials on how stuff like buttons work in AS3, you’re wasting your time and ours if all you’re gonna show is how ONE BUTTON works. Or how putting buttons on different keyframes don’t work. OR, even something as basic as wether you’re using a mc(MovieClip) or a an actual button component for a button(and that you have to name them).
/END RANT
Danielle
Hello All…I am pulling my hair out…PLEASE HELP!!!
I am trying to add URL navigation to my menu buttons. I followed a tutorial to create a sliding menu bar (http://schoolofflash.com/2008/05/flash-cs3-tutorial-sliding-menu-bar/). Menu works great.
When I try to add the above script to add in the URL navigation I keep getting errors. I get the same errors if I add this script to the “Buttons” layer in the action panel or the “Actions” layer in the actions panel.
The errors I get are:
1093: syntax error (show up 3 times)
1084: syntax error: expecting rightparen before colon (shows up once)
Thanks…I appreciate any ideas you have
(Except from “dan”, sorry man but I can’t understand anything you write)
Danielle
Thanks to no one but myself…this code works with NO ERRORS…I couldn’t tell you why this works for me and not the code in this tutorial…
If you need more URL’s add numbers (1, 2, 3 so on) to the “thumbDownHandler” or “thumbUpHandler” anywhere it appears. It will look like so…”thumbDownHandler2″.
Below is the code:
home_mc.addEventListener(MouseEvent.MOUSE_DOWN, thumbDownHandler);
function thumbDownHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(“http://msdn2.microsoft.com/en-us/silverlight/default.aspx”));
}
sports_mc.addEventListener(MouseEvent.MOUSE_DOWN, thumbUpHandler);
function thumbUpHandler(event:MouseEvent):void {
navigateToURL(new URLRequest(“http://www.adobe.com”));
}
poker_mc.addEventListener(MouseEvent.MOUSE_DOWN, thumbDownHandler2);
function thumbDownHandler2(event:MouseEvent):void {
navigateToURL(new URLRequest(“http://www.hotmail.com”));
}
I hope this helps!!
srinivas_dikonda
i want request for actionscript for flash menu like “home” “contact” “sitemap” to url links to web page in internal link like if i open “home page” it link to index.html & if i link to contact links to contact & same on sitemap also like it dynamic page please mention me script for detail of this……..
Jan
THANKS for this. I’m still figuring out as3, very slowly. Sites like this are making it possible to get things done. : )
I thought someone might find this helpful if you want the hand cursor. Put the following code after
myButton.addEventListener(MouseEvent.CLICK,onMouseClick);
// if you want a hand cursor
myButton.buttonMode = true;
myButton.useHandCursor = true;
Happy coding
valt
For those of you having problems with your URL’s opening up in a new window even when you delete the line of code, dont delete it! Instead, replace _blank with _self. I notice by default action script opens up URL’s in a new window. See if that works.
Pinoy Gamers
I have the same problem as above, but maybe… your topic will help me.
LH
Hi, I got the button to work, but want it to stay within the same browser instead of opening a new one
I tried leaving the “_Blank” part out as you suggested, but it still seemed to open a new window.
What am I doing wrong?
LH
Ok i am an idiot, i just read the post above and it instantly solved my problem. Sorry and thank you.
Ed
Just wasted an hour and a half trying to figure something out that usually takes 5 seconds, and I still can’t get it to work! THANKS ADOBE!!!!!
Bernie C
How do I use this code for multiple URLs. In AS3 you use to be able to put as many links as you wanted to. But doing it now I keep getting errors that I’m duplicating code:
function onMouseClick(e:MouseEvent):void
KD
I am making Flash file in Action 3.0.
This CD contains 5 PDF product files and 1 PDF profile file.
I want to open Profile PDF file in Flash SWF file itself
AND
Rest 5 PDF files I want to open in New Window above the Flash exe. I don’t want to close Flash Main file.
I want user to insert CD in CD Drive and It Automatically open through Auto-run file.
When viewer click on PDF button.
Then PDF file should be open above the Flash exe. After viewing the PDF, viewer close the PDF and View Another PDF file.
Pleas help me..
It’s a question of my Job.
KD
Currently I am using this for opening PDF file:
PELpro_btn.addEventListener(MouseEvent.CLICK, pClick);
function pClick(e:MouseEvent):void
{
var url:String = “PEL-Profile.pdf”;
var req:URLRequest = new URLRequest(url);
navigateToURL(req, “_parent”);
}
Help me….
James
When will Adobe realise that most people ‘trying’ to use Flash are not programmers with !st Class Degrees! A lot of Flash users are designer/artistic types who learn As1 and AS2 quite easily to build their projects/sites. AS3 is going to force these types of user to stick with AS2 for as long as possible, but the question is, how long will the Flash Player support AS2 ! ? What the heck are Adobe trying to achieve by making such mundane simple tasks, only accomplishable by programming types? Unbelievable!
Duncan
HI,
Please can someone help!!!
I am trying to put a .flv import file onto a page of my website in dreamweaver and what i want to do is to get it to automatically navigate to a different page once the movie has finished playing. The movie is setup as autoplay and autorewind with no click or button commands.
I am a bit of a novice at actionscript 3 so i am looking for someone to guide me through the process (instances etc…)
Many Thanks to whoever may take this task
Duncan
Ashley Voigt
“How to play Flash movie in a browser and then at end of movie go to specific URL [without loading new browser window].”
I am fairly inexperienced with Flash. This site has provided the answer for ActionScript 3 for me. Thank you guys.
This worked for me:
Create new layer. Go to end of movie/timeline and then Insert/Timeline/Blank Key Frame and attach action script to it.
Window/Actions.
insert the following:
stop()
var request:URLRequest = new URLRequest(“replace_this text_in_quotes_with_your_URL”);
navigateToURL(request,”self”);
Good luck.
ASHLEY
Ashley Voigt
I just tested this code in Firefox. Bummer. If pop ups are set to block in Firefox then this code does not work.
bryan
I get an error. What’s up? This is the same error commented above by Turin in June 2008, but I see no reply to that error message.
It says “‘{‘ expected” and points to the error in the line function onMouseClick(e:MouseEvent):void
The full code is here:
GoButton.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://nursing.umaryland.edu/admissions/open.htm”);
navigateToURL(request);
}
Can someone tell me what’s up?
Alexandre de Oliveira
For those of you having this problem:
“1120: Access of undefined property myButton.”
I had the same problem, then I found out how to solve this one. Click on the Button you just converted to, look at the “Properties” Panel, then look for an input that has written “”. Rename it to whatever you want, like “myButton”, for example. This name will be readable by the ActionScript.
Ashley Voigt
“How to play Flash movie in a browser and then at end of movie go to specific URL [without loading new browser window].”
Refer 83, above.
Alternative way of getting the action to work.
Create new layer. Go to end of movie/timeline and then Insert/Timeline/Blank Key Frame and attach action script to it.
stop()
Create another new layer. Go to end of movie/timeline and then Insert/Timeline/Blank Key Frame and attach action script to it.
var request:URLRequest = new URLRequest(“replace_with_name_of_your.html”);
navigateToURL(request, “_self”)
Unfortunately, Firefox still views this coding as a pop up.
Can anyone overcome this pop up issue in Firefox?
stas
navigateToURL(request,”_top”)
worked for me.
Ashley Voigt
navigateToURL(request, “_self”)
I just tried the site again.
This does not create a pop up problem in Firefox any more. Has there been an update in Firefox or something?
The code at comment No.89 does work.
Refer http://www.buildingondesign.com.au for example.
Travis
Having some trouble with this code:
theBTN1.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(“http://designsearch.renze.com/”);
navigateToURL(request,”_parent”);
}
It works fine when I test the movie. But after I publish it, the link no longer functions. Help!
Michelle
<3 thanks so much for this. I am new to flash and I’ve been looking for this code for days!!! and it work!! TY!
doug
this was quite helpful. I wish I would have scrolled down to the comments so i could have just copy and pasted the code. oh well…
how to you make a comments section like this one anyway??
Chago
I think i am the newest here but here i go ,,,, i got this working and is fine but there is a way to make it more simple ……
banner_mc.buttonMode = true;
logo_mc.buttonMode = true;
mini_principal_mc.buttonMode = true;
mini_somos_mc.buttonMode = true;
banner_mc.addEventListener(MouseEvent.CLICK,clickHandler1);
function clickHandler1(event:MouseEvent):void
{
navigateToURL(new URLRequest(“index.html”),”_self”);
}
logo_mc.addEventListener(MouseEvent.CLICK,clickHandler2);
function clickHandler2(event:MouseEvent):void
{
navigateToURL(new URLRequest(“index.html”),”_self”);
}
mini_principal_mc.addEventListener(MouseEvent.CLICK,clickHandler3);
function clickHandler3(event:MouseEvent):void
{
navigateToURL(new URLRequest(“index.html”),”_self”);
}
mini_somos_mc.addEventListener(MouseEvent.CLICK,clickHandler4);
function clickHandler4(event:MouseEvent):void
{
navigateToURL(new URLRequest(“somos.html”),”_self”);
}
i keep reading but is hard sometimes to understand ,,,, sorry but i hope nobody get mad for this ,,,, thank you in advance ,,,,, hey craig you may recognize the address i got the beggining but can no purchase the others ,,,,, since the lost of the legs in accident is been hard to get some cash to spend ,,,, thank you for the good work,,,,,
Marcia
I cannot get this code to work for the life of me. I have copied it EXACTLY and still I can’t get it to work? I have even been using the generic adobe URL just to try to get it to work? I have now spent several hours just trying to add a simple link! Someone PLEASE help this Flash newbie.
Here is my code…
this.navBar.navPortfolio_mc.rollDownPortfolio_mc.subMenu1.addEventListener(MouseEvent.CLICK, onMouseClick);
function onMouseClick(e:MouseEvent):void
{
var request:URLRequest=new URLRequest(“http://www.adobe.com”);
navigateToURL(request, “_self”);
}
Chago
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