Scriptplayground Network

Scriptplayground Preview
Fireworks CS4 Learning Center

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. This book explains the process of working with PHP in Flash, while creating real world examples that you can actually learn something from.

The Flash and PHP Bible has a dedicated forum for support and comments.

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.

Comments: Opening Url in ActionScript 3 dynamically

 Jonathon  Sat May 3, 2008 2:13 pm  
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 2:51 pm  
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 5:50 am  
Very Nice this will be very useful.
 wagner guatimozim  Thu May 15, 2008 8:44 am  
You save my life! :)
Thank You!
 Dave  Thu May 15, 2008 9: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 9: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 9: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.
Add a comment
Name:
Website:
Comment:
Please note: Offensive comments, flaming and spamming is not permitted on this site and your comment will be deleted immediately.

HTML is not allowed.

Please provide all comments in English so that others can help you. A common helper in this is to use an online translator.

As a security measure your ip will be recorded.
 
Anti-Robot Check:

Enter the key you see above.

What is this?: This extra test has been added due to the recent explosion of spam.
 
Google