Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » as » Opening Url in ActionScript 3 dynamically

Opening Url in ActionScript 3 dynamically

How to open a URL dynamically using a custom class.

In a previous tutorial, we learned how to open url's using ActionScript 3. However a few people have had problems opening different links in a dynamic way. There are a couple of ways to accomplish this. One way is to create a function for each link. The other, more reusable way is to create a custom class. We will work from the second option in this tutorial.

The first part is to create the ActionScript that will be reused. Start by creating a new ActionScript file by going to New > ActionScript File. Save this file with the name "CustomURLNavigator". Once the ActionScript file is created you can create the custom class.

package 
{
        public class CustomURLNavigator
	{
		
	}
	
}

Once the package is defined in the class, the next step is to import the necessary packages.

package 
{

        import flash.net.navigateToURL;
        import flash.net.URLRequest;
        import flash.events.Event;
	
        public class CustomURLNavigator
	{
		
	}
	
}

After you have completed the skeleton of the class, the final step is to add in the code specific to this application. Which in this case would consist of two variables to store the link and target, as well as a method that actually does the link calling.

package 
{

        import flash.net.navigateToURL;
        import flash.net.URLRequest;
        import flash.events.Event;
	
        public class CustomURLNavigator
	{
		public var url:String;
		public var target:String;
		
		function CustomURLNavigator(u:String, t:String="_blank")
		{
			url = u;
			target = t;
		}
		
		public function callLink(e:Event):void
		{
			var r:URLRequest = new URLRequest(url);
			navigateToURL(r, target);
		}	
	}
	
}

At this point you should have the completed class. In future tutorials you will see a greater attention to detail on class building, but that seemed a bit off-topic for this tutorial.

Moving on, the next step is to create a new Flash File (FLA) to test out the class. To start with, we will just create a simple pure ActionScript example.

Open the Actions panel (F9, Window > Actions) and add the following code. This code will import the new class, and test the code by opening a link to Google in a new window.

import CustomURLNavigator;

var url:String = "http://www.google.com";
var customURLNavigator:CustomURLNavigator = new CustomURLNavigator(url);

customURLNavigator.callLink(null); // "null" added to satisfy Event

When you test the movie (CTRL/CMD + ENTER) you should see a new window opened, showing Google's home page. This shows the code is working properly, but probably isn't the most useful application since it opens the link immediately.

Custom URL called by Event

To attach this code to a button or other event object, you would simple add in the class instance and method name as the "function" argument.

import CustomURLNavigator;

var url:String = "http://www.google.com";
var customURLNavigator:CustomURLNavigator = new CustomURLNavigator(url);

homeBtn.addEventListener(MouseEvent.CLICK, customURLNavigator.callLink);

That is all there is to creating a custom class to call links dynamically. Hope you found this tutorial useful. Please leave a comment or question below.

Update: Here is another custom class, developed by Alan which features a one-line use and ExternalInterface support.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Opening Url in ActionScript 3 dynamically

 Jonathon  Sat May 3, 2008 10:13 am  
Thanks for your dedication to this. I can see how this might work, but don't see how it's dynamic. What would have to be added if we wanted an "awayBtn" button set up to guide the user to yahoo.com? Wouldn't

awayBtn.addEven...blahblahblah...tor.callLink);

also go to google?

Also, is setting up an external custom class a necessity? In the previous tutorial you set it up as a function within the FLA file.
 mkeefe  Sat May 3, 2008 10:51 am  
The advantage here is with two lines of code you create a new link.


var customURLNavigator2:CustomURLNavigator = new CustomURLNavigator("http://www.yahoo.com");
homeBtn.addEventListener(MouseEvent.CLICK, customURLNavigator2.callLink);


The function in the previous tutorial would actually need more code each time. Also this "class-based" version is more reusable without even having to touch the URL logic.

..but it is NOT required, just a best practice.
 Endyoursearch  Mon May 5, 2008 1:50 am  
Very Nice this will be very useful.
 wagner guatimozim  Thu May 15, 2008 4:44 am  
You save my life! :)
Thank You!
 Dave  Thu May 15, 2008 5:02 pm  
Hi, I'm trying to put together a Flash project... I want to be able to click a button, triggering an animation, then having the flash project head to a different site. I haven't been able to do it so far.

Basically, I'm wondering... How do I pass the URL from one frame to the last frame of the animation?
 Dave  Thu May 15, 2008 5:08 pm  
Oh, almost forgot.. I've gotten these errors:

1120: Access of undefined property url.

Which is where the URL is failing to pass.

1151: A conflict exists with definition url in namespace internal.

Where I can pass a sinlge URL to the final frame, but setting multiple URLs up for multiple links causes it to fail.
 mkeefe  Thu May 15, 2008 5:58 pm  
@Dave, If you define a variable on frame 1 it will be accessible on any frame within the same scope. You can also create an ENTER_FRAME event handler that is called on each frame.

Something like:


stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

function enterFrameHandler(e:Event):void {
if(currentFrame == 20) { // 20 is the specified frame
// url call or other code here
}
}


Part 2: The error you receiving is because your using a reserved word. Use something like "myURL" or something similar.
 mayuri  Wed Jul 30, 2008 3:15 am  
url
 wdag  Sat Aug 2, 2008 12:26 pm  
Hi, you guys are doing a terrific job, tnxs a lot. I've got problem understanding how begin and stop to write my code in classes. for instance, I want to my movie moves and stop draging when a Mouse event occurs on it. I've got my fla. and my movie (mygallery) within. then I assigned my document to my main.as in which the fallow code is wrote.

mygallery.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
mygallery.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
function onStartDrag(evt:MouseEvent):void {
evt.target.startDrag();
}
function onStopDrag(evt:MouseEvent):void {
evt.target.stopDrag();
}

To summon all, how I've to begin writing my code and end up it in order to this ONLY code works?.

Tnxs.
 wdag  Sat Aug 2, 2008 12:38 pm  
Sorry but I've try with package
{Code} and I've still having this error:

1046: type was not found or was not a compile-time constant:MouseEvent. on this two lines:

function onStartDrag(evt:MouseEvent):void {
function onStopDrag(evt:MouseEvent):void {

tnxs for any help. as I said, it must be cause I'm a newbie and I don't know how to begin and end up writing my code.

 mkeefe  Sat Aug 2, 2008 1:01 pm  
@wdag - Make sure you have imported the MouseEvent class. The error your showing there leads me to believe you probably haven't... or so I think.

Hope that helps,

Matt
 Guest  Sat Aug 2, 2008 2:23 pm  
Also I've wrote the code not in a document class but in the frame in which my "Gallery" is embed but the "1120 Acces of undefined property myGallery" pop ups. What am I doing wrong?.

Frame = Actions = I've wrote:

myGalley.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
myGallery.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
function onStartDrag(evt:MouseEvent):void {
evt.target.startDrag();
}
function onStopDrag(evt:MouseEvent):void {
evt.target.stopDrag();
}
 wdag  Sat Aug 2, 2008 2:24 pm  
Sorry, I aint saw your respond, tnxs for the help, Im gonna to chek it out.
 wdag  Sat Aug 2, 2008 2:45 pm  
Well, I wrote this code in my document class called Main:

package
{

import flash.display.MovieClip;
import flash.MouseEvent;

public class Main
{

}

}

Then I wrote this code in the frame with my Movie Clip "myGallery":

myGallery.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
myGallery.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
function onStartDrag(evt:MouseEvent):void {
evt.target.startDrag();
}
function onStopDrag(evt:MouseEvent):void {
evt.target.stopDrag();
}

And errors 1046 appears again plus 5000 error:

The class "Main" must subclass "flash.display.MovieClip" since it is linked to a library symbol of that type.

Tnxs for any help
 mkeefe  Sat Aug 2, 2008 2:47 pm  
import flash.display.MovieClip;

public class Main extends MovieClip

(sorry for the short response, I am remote)
 wdag  Sat Aug 2, 2008 2:54 pm  
Then just to see what happens I wrote this code in my main class:

package
{

import flash.display.MovieClip;
import flash.MouseEvent;

public class Main
{

myGallery.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
myGallery.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);

function onStartDrag(evt:MouseEvent):void {
evt.target.startDrag();
}

function onStopDrag(evt:MouseEvent):void {
evt.target.stopDrag();
}

}

}

Same errors shown up.
 wdag  Sat Aug 2, 2008 3:14 pm  
tnxs for your time, well I added "public class Main extends MovieClip" to my code, but some errors shown up.

This is my code in my main class:

package
{

import flash.display.MovieClip;
import flash.MouseEvent;

public class Main extends MovieClip
{



}

}

And this is my code in my Movie:

myGallery.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
myGallery.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);

function onStartDrag(evt:MouseEvent):void {
evt.target.startDrag();
}
function onStopDrag(evt:MouseEvent):void {
evt.target.stopDrag();
}

1046 still there.

Dont worry but short answers, any help is much apreciated.
 mkeefe  Sat Aug 2, 2008 3:15 pm  
Try placing all the code in the Document Class, it sounds like you may be placing some on the timeline and other in the class file.
 Guest  Sat Aug 2, 2008 3:29 pm  
This is my new code:

package
{

import flash.display.MovieClip;
import flash.MouseEvent;

public class Main extends MovieClip
{
myGallery.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
myGallery.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);

function onStartDrag(evt:MouseEvent):void {
evt.target.startDrag();
}

function onStopDrag(evt:MouseEvent):void {
evt.target.stopDrag();
}

}

}

1046 still happens.

Tnxs for ur time.
 Said Bakr  Sun Aug 17, 2008 1:12 pm  
I wonder, if is it possible to use Actionscript to printing an HTML page and returns the printer status, i.e. user's printer completed the printing task or not.

The javascript print() method is limited to intialize the printer box of the browser while it has no any control about the page is comletely printed or not, so I tried to find such featue in flash.
 mkeefe  Sun Aug 17, 2008 7:38 pm  
@Said Bakr - As far as I know, the print abilities of Flash are limited to sending the data out.. but not truly knowing if it printed. The OS would handle that, if possible.
 matt  Tue Oct 14, 2008 6:54 am  
works great and beats the heck out of creating a function for every link. thanks for the help!
 Milly  Sat Dec 6, 2008 7:14 pm  
Hi,

I tried your tutorial and it worked! Great job! Thanks!
However, I tried it on my own flash file and i'm having great difficulty solving the problem.

I have a movieclip within a moviclip and within that are my buttons (perhaps bad design, im new to this???)

When I click on the first movieclip it plays. However, when I enter the above code the movieclip automatically starts and the link doesn't work!

Any suggestions....are much welcome!

Thanks
 mkeefe  Mon Dec 8, 2008 10:50 am  
@Milly - It sounds like you may be calling the function directly, rather than attaching it to an event handler.
 Evan  Thu Dec 11, 2008 5:56 pm  
Thanks so much for this tutorial. Got my first invisible button working using this code.

I've got a question. I'm doing a sliding banner ad and I want to change the URL associated with the CLICK event for my button at two different points in my timeline.

How would I do this using the CustomURLNavigator class?
 mkeefe  Sun Dec 21, 2008 2:20 pm  
@Evan - All you need to do is change the value of the url property located within the customURLNavigator class.

Such as:


customURLNavigator.url = "http://example.com/newpage";


Matt
 Mike  Fri Jan 23, 2009 9:49 am  
Hey, thanks for putting this up... it was exactly what I needed and worked flawlessly.
 Jon  Mon Feb 23, 2009 11:51 am  
Thank you, Matt. Helpful code and well explained.
 alex  Fri Jul 10, 2009 5:23 pm  
Hi Matt, your tutorial is very usefull for newbies such as myself, who are still trying to figure out how classes really work.
I was wondering why "CustomURLNavigator" is a normal function and "callLink", on the other hand, is a public function. I guess, my question is when do functions inside classes need to be public.
thanks
Alex
 mkeefe  Mon Jul 13, 2009 7:55 am  
@alex - The "CustomURLNavigator" is the constructor (function automatically called when the class is run. The constructor does not have an access type because it can never be run directly. However "callLink" can be run from outside the class so its set as public. There are other options too, such as: "private", "final", "dynamic" to name a few. These access modifiers let you the developer determine who can access your functions in the class. Normally internal (within the class) operations are set to "private" so external code can't alter your class process.
 alex  Tue Jul 14, 2009 2:42 pm  
Hi Matt, thanks for the clarification.
 doogiedesign  Wed Oct 7, 2009 4:40 am  
Thanks for sharing this. I know I'll be using it a lot!
 joan anderson  Fri Oct 16, 2009 12:37 pm  
I need to add this code to my pages in order to link my flash site to external url addresses. However because I am using an object oriented scroll bar and have my site already linking to external classes this code states that one of my classes does not recognize the customURLNavigator and I am receiving errors. I would much appreciate a reply as I need to my site to link to external sites.
 joan  Thu Oct 22, 2009 8:10 am  
read thru your comments. answer was there.
thanks,
 shashank  Fri Jan 8, 2010 6:03 pm  
thanks for the help...
but i wanted to know if we could call 2 or urls at a time by giving them some alias name..if would be great if u explain with a example
 Guest  Tue Aug 30, 2011 11:49 pm  
@mkeefe u r a lifesaver! thx all for the great posts!
 Maija  Thu Jan 5, 2012 2:00 pm  
I'm at my wits end. I used to do a lot of AS3 scripting but I've been out of it for a couple of years and maybe browsers have changed since then, because I don't remember having this issue before. I used the script you've provided here to ensure that the problem wasn't in my script, but the problem persists, so it must be something else. This is the problem: once I've used a Flash button to navigate to a URL, if I use the browser's "Back" button to return to the page with the flash button on it, the buttons no longer work. None of them. The rollover states are also either stuck "on" or not working if they're not on. It's like the Flash no longer has focus, but I'm clicking the crap out of it. I have to refresh the page to get the buttons working again. This is in FF 9. Any suggestions? Cheers.
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles