Scriptplayground

 Print Page | Close Window

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.


Find this article at:
http://scriptplayground.com/tutorials/as/Opening-Url-in-ActionScript-3-dynamically/