Home » ActionScript 3.0, Flash AS 3, Flex 3, _Featured

Define and call javascript functions directly inside an AS3 class (FL / FX)

12 September 2009 One Comment

ActionScript 3.0 doesn’t manage all developers needs, so often they have to use other languages, as JavaScript, to accomplish some operations like open an HTML popup or get the userAgent property, useful to know the user Browser.

The AS3 ExternalInterface class allow to invoke Javascript functions directly from Flash.
Usually these functions are defined inside the HTML page but in this sample we’ll see how to implement them inside an ActionScript class, avoiding to edit HTML code.

Check the result:

This movie requires Flash Player 10

Il file mxml principale

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
	layout="vertical">
 
	<mx:Script>
		<![CDATA[
			import com.fabiobiondi.utils.URLUtil;
		]]>
	</mx:Script>
 
	<mx:TextInput id="browserType" width="250" height="20" />
 
	<mx:Button label="checkBrowser" 
			   click=" browserType.text =  URLUtil.checkBrowser()"/>
 
	<mx:Button label="openPopup" 
			   click=" URLUtil.openWindow(
   						  'http://www.google.com',
   						  '_blank', 
   						  'width=400,height=200,scrollbars=yes')" />
 
</mx:Application>

La classe URLUtil
package: com.fabiobiondi.utils

package com.fabiobiondi.utils {
 
    import flash.external.ExternalInterface;
 
    public class URLUtil    {
 
	/**
	 * Open a popup window
	 * @param	url			link to open
	 * @param	target		window target
	 * @param	options 	popup options
	 * 
	 */
        public static function openWindow( url : String, 
        				    target : String = "_blank", 
        				    options : String = "") : void {
 
            ExternalInterface.call("window.open", url, target, options);
        }
 
 
	/**
	 * Get the Browser Type
	 * @Return the userAgent property	
	 * 
	 */
	public static function checkBrowser():String {
 
		var userAgent:String = 
			String(ExternalInterface.call("function() {return navigator.userAgent;}"));
 
		return userAgent;
 
 
	}
 
 
    }
 
}

This is a cool trick I have found surfing the web and, even if this technique is not applicable when large javascript functionaties are needed, I think it could be very useful to manage standard situations and to create a js actionscript library to quickly use in every your project.

TIPS: if we don’t pass the options parameter in the openPopup method we’ll open a new window instead of a popup. This could be another way to open html windows, instead using classic navigateToURL that sometimes generate some problems with the “browser popup block”

HOW TO TEST THIS CODE:
Even if i use Flex to create this sample, you can use the same AS3 class in Flash CS*.

For Flash Users: it’s not possible test this scripts inside your Flash enviroment. You need to create the HTML container (using publish settings) and test it from a local or remote webserver.

For Flex Users: HTML is automatically generated but you need to test in a local/remote webserver too.

Download Source Code (FX3)

One Comment »

  • Fabio Biondi (author) said:

    On our italian community forum (www.actionscript.it/forum) an user has requested a way to get the url path of the SWF Container (usually an HTML file).

    This is another JavaScript task, so following the new method for the URLUtil class to accomplish this task:

    /**
    * Get the absolute path of the HTML Container
    * @Return the path
    *
    */

    public static function getPathHTMLContainer():String {

    var path:String =
    String(ExternalInterface.call(”window.location.href.toString”));

    return path;

    }

    USAGE (in Flash o Flex):
    outputTxt.text = URLUtil.getPathHTMLContainer()

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.