Quick Tip: How to Communicate Between Flash and JavaScript
In this Quick Tip, we'll look at how to use the ExternalInterface class. This allows us to write AS3 which can run JavaScript code, and vice-versa. That means you can use Flash to alter parts of the webpage in which it's running!
Step 1: Set up the Flash Document
Create a new Flash ActionScript 3 document. Resize the stage to be 600x300. With the rectangle tool, draw out a rectangle that is the size of the stage. Give it a color of #CCCCCC. Also, give it a black stroke of 2px.



Step 2: Set up the Flash UI
Here's the layout we'll be working towards:



Open the Components Panel (Window > Components) and, from the User Interface folder, drag a ColorPicker component onto the stage. Give it an instance name of 'cp'.
Next create a dynamic text field called 'resizeText'; place and size it however you please (you can't see the one in my image; it's empty, and in the top-right of the stage.)
Now, create another dynamic text field. Give it an instance name of 'jsText'. Then create a button symbol and give it an instance name of 'prompt'. After that, create another button and give it an instance name of 'change'.
Finally, create two input text fields. Place one next to your 'prompt' button, and give it a name of 'promptText'. Take the second text field, move it next to your 'change' button and name it 'changeText'.
Also, add any labels you want; refer to my image to see how I set it up.
Step 3: Set up the HTML UI
In order for the ExternalInterface to work, the document has to be on the internet. First, create a new text file, and save it as 'externalInterface.html'. Next, open a text editor and add all the code below. Save the HTML file.
1 |
|
2 |
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
3 |
<head>
|
4 |
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> |
5 |
<title>externalInterface</title> |
6 |
<style type="text/css"> |
7 |
|
8 |
body { |
9 |
font-family:Arial; |
10 |
}
|
11 |
|
12 |
#asSend { |
13 |
padding-top:20px; |
14 |
font-size:12px; |
15 |
}
|
16 |
#htmlWrap { |
17 |
margin-top:10px; |
18 |
width:578px; |
19 |
padding-left:20px; |
20 |
border-width:1px; |
21 |
border-style:solid; |
22 |
}
|
23 |
#sender { |
24 |
margin-top:10px; |
25 |
}
|
26 |
#textChange { |
27 |
font-size:13px; |
28 |
font-weight:bold; |
29 |
padding-top:10px; |
30 |
padding-bottom:20px; |
31 |
}
|
32 |
|
33 |
</style>
|
34 |
</head>
|
35 |
<body>
|
36 |
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="600" height="300" id="externalInterface" align="middle"> |
37 |
<param name="allowScriptAccess" value="sameDomain" /> |
38 |
<param name="allowFullScreen" value="false" /> |
39 |
<param name="movie" value="externalInterface.swf" /> |
40 |
<param name="quality" value="high" /> |
41 |
<param name="bgcolor" value="#ffffff" /> |
42 |
<embed src="externalInterface.swf" quality="high" bgcolor="#ffffff" width="600" height="300" name="externalInterface" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> |
43 |
</object>
|
44 |
<div id="htmlWrap"> |
45 |
<div id="asSend"> |
46 |
<label for="textArea">Send to actionscript:</label><br /> |
47 |
<textarea cols="50" rows="4" id="textArea" name="textArea"></textarea> |
48 |
<br /> |
49 |
<button id="sender" name="sender">Send</button> |
50 |
</div>
|
51 |
<div id="textChange">Use Actionscript to change me!</div> |
52 |
</div>
|
53 |
</body>
|
54 |
</html>
|
The key areas are:
- The <object> section, which embeds the SWF you'll create from the Flash file.
- The <div>s and <textarea>, which have id properties so that we can access them from the SWF.
Your HTML should appear as below:



When the code has been replaced, upload the file to your webserver, so we can get started with the ActionScript.
Step 4: Set up a Document Class
Create a document class for your Flash file; call it Main.as. If you're not familiar with document classes, read this Quick Tip.
1 |
|
2 |
package
|
3 |
{
|
4 |
import flash.display.MovieClip; |
5 |
public class Main extends MovieClip { |
6 |
public function Main() { |
7 |
|
8 |
}
|
9 |
}
|
10 |
}
|
Step 5: Calling JavaScript Functions from Flash
The first thing we'll do with the ExternalInterface is call a JavaScript function that will change the background color of our webpage. So, attach an event listener to our ColorPicker component. When the color changes, it will send the hex value to a javascript function called receiveColor():
1 |
|
2 |
package
|
3 |
{
|
4 |
import fl.events.ColorPickerEvent; |
5 |
import flash.display.MovieClip; |
6 |
import flash.external.ExternalInterface; |
7 |
public class Main extends MovieClip { |
8 |
public function Main() { |
9 |
cp.addEventListener(ColorPickerEvent.CHANGE, colorChange); |
10 |
}
|
11 |
|
12 |
public function colorChange(event:ColorPickerEvent):void { |
13 |
ExternalInterface.call("receiveColor", event.target.hexValue); //calls receiveColor(event.target.hexValue) in the javascript |
14 |
}
|
15 |
}
|
16 |
}
|
Now we have to write this receiveColor() function. In the head of our HTML document, we start the javascript by defining this function. It simply takes the value sent to it from Flash and changes the background color.
1 |
|
2 |
<script type="text/javascript"> |
3 |
function receiveColor(value) { |
4 |
document.bgColor = "#" + value; |
5 |
}
|
6 |
</script>
|
Put that right after <head> in your HTML file. If all's well, when you run the HTML page in a browser and change the color in the ColorPicker, it should change the background color of the webpage.
Step 6: Calling ActionScript Functions from JavaScript
The next example will be to send data from JavaScript to Flash. In the HTML document, paste the following code within the <script> tag you added in the last step:
1 |
|
2 |
window.onload = function() { |
3 |
var sender = document.getElementById("sender"); //getElementById finds an element according to its "id" property |
4 |
sender.onclick = function() { |
5 |
var ta = document.getElementById("textArea"); |
6 |
document['externalInterface'].receiveText(ta.value); |
7 |
ta.value = ""; |
8 |
}; |
9 |
}; |
Here's what this does: after the document is loaded, we get the 'sender' button and attach an event listener to it. When the 'sender' button is clicked, it will call the receiveText() function in Flash that we will set up now.
Back in Flash, we tell the ExternalInterface to register the ActionScript function so that it can be called from JavaScript. Then we set up our receiveText() function:
1 |
|
2 |
package
|
3 |
{
|
4 |
import fl.events.ColorPickerEvent; |
5 |
import flash.display.MovieClip; |
6 |
import flash.external.ExternalInterface; |
7 |
public class Main extends MovieClip { |
8 |
public function Main() { |
9 |
cp.addEventListener(ColorPickerEvent.CHANGE, colorChange); |
10 |
ExternalInterface.addCallback("receiveText", receiveText); //allows JavaScript to access the receiveText() function. |
11 |
}
|
12 |
|
13 |
public function colorChange(event:ColorPickerEvent):void { |
14 |
ExternalInterface.call("receiveColor", event.target.hexValue); |
15 |
}
|
16 |
|
17 |
//this is the new receiveText function
|
18 |
public function receiveText(value:String):void { |
19 |
jsText.text = value; |
20 |
}
|
21 |
}
|
22 |
}
|
(New lines are 5, 9 and 16-19.)
Step 7: Calling JavaScript Alerts, Confirms and Prompts from ActionScript
We can also call alerts very easily from ActionScript. Here we simply tell the ExternalInterface to call a 'prompt'. We can also use the ExternalInterface to pass parameters to functions. Here we tell the 'prompt' function to ask the user his or her name. When our user enters the info, it's passed back to the 'promptText' text field.
1 |
|
2 |
package
|
3 |
{
|
4 |
import fl.events.ColorPickerEvent; |
5 |
import flash.display.MovieClip; |
6 |
import flash.external.ExternalInterface; |
7 |
public class Main extends MovieClip { |
8 |
public function Main() { |
9 |
cp.addEventListener(ColorPickerEvent.CHANGE, colorChange); |
10 |
ExternalInterface.addCallback("receiveText", receiveText); |
11 |
prompt.addEventListener(MouseEvent.CLICK, promptClick); //makes promptClick() run when prompt button is clicked |
12 |
}
|
13 |
|
14 |
public function colorChange(event:ColorPickerEvent):void { |
15 |
ExternalInterface.call("receiveColor", event.target.hexValue); |
16 |
}
|
17 |
|
18 |
public function receiveText(value:String):void { |
19 |
jsText.text = value; |
20 |
}
|
21 |
|
22 |
//function to be called when prompt button is clicked. Will ask for user's name using a JS prompt.
|
23 |
public function promptClick(event:MouseEvent):void { |
24 |
promptText.text = "You said your name is: " + ExternalInterface.call("prompt", "What is your name?"); |
25 |
}
|
26 |
}
|
27 |
}
|
(New lines are 10 and 21-24.)
Step 8: Calling Anonymous JavaScript Functions
Another thing we can do is write our own JavaScript functions as strings, then call them from the ExternalInterface. Here we create a JavaScript function that receives a parameter. We take that parameter and assign its value to the innerHTML attribute of our 'textChange' div in the HTML document. You'll notice that there are no external JavaScript functions being called - it is all contained within the ActionScript.
1 |
|
2 |
package
|
3 |
{
|
4 |
import fl.events.ColorPickerEvent; |
5 |
import flash.display.MovieClip; |
6 |
import flash.external.ExternalInterface; |
7 |
public class Main extends MovieClip { |
8 |
public function Main() { |
9 |
cp.addEventListener(ColorPickerEvent.CHANGE, colorChange); |
10 |
ExternalInterface.addCallback("receiveText", receiveText); |
11 |
prompt.addEventListener(MouseEvent.CLICK, promptClick); |
12 |
change.addEventListener(MouseEvent.CLICK, changeClick); //makes changeClick() run when change button is clicked |
13 |
}
|
14 |
|
15 |
public function colorChange(event:ColorPickerEvent):void { |
16 |
ExternalInterface.call("receiveColor", event.target.hexValue); |
17 |
}
|
18 |
|
19 |
public function receiveText(value:String):void { |
20 |
jsText.text = value; |
21 |
}
|
22 |
|
23 |
public function promptClick(event:MouseEvent):void { |
24 |
promptText.text = "You said your name is: " + ExternalInterface.call("prompt", "What is your name?"); |
25 |
}
|
26 |
|
27 |
//changes text inside the HTML to match text field inside Flash
|
28 |
public function changeClick(event:MouseEvent):void { |
29 |
ExternalInterface.call("function(param){ document.getElementById('textChange').innerHTML = param; }", changeText.text); |
30 |
changeText.text = ""; |
31 |
}
|
32 |
}
|
33 |
}
|
(New lines are 11 and 26-30.)
Step 9: Calling Anonymous JavaScript and ActionScript Functions
Finally, we can call anonymous functions on both sides. In the 'anonymous' function, we register an anonymous ActionScript function with the ExternalInterface. The function fills in some text, then starts a timer. Next, we call an anonymous JavaScript function. This function tells the window, when it's been resized, it must call back to our anonymous ActionScript function.
1 |
|
2 |
package
|
3 |
{
|
4 |
import fl.events.ColorPickerEvent; |
5 |
import flash.display.MovieClip; |
6 |
import flash.external.ExternalInterface; |
7 |
public class Main extends MovieClip { |
8 |
public function Main() { |
9 |
cp.addEventListener(ColorPickerEvent.CHANGE, colorChange); |
10 |
ExternalInterface.addCallback("receiveText", receiveText); |
11 |
prompt.addEventListener(MouseEvent.CLICK, promptClick); |
12 |
change.addEventListener(MouseEvent.CLICK, changeClick); |
13 |
|
14 |
//create a new timer with a one second tick, and add an event listener
|
15 |
var timer:Timer = new Timer(1000); |
16 |
timer.addEventListener(TimerEvent.TIMER, onTimer); |
17 |
|
18 |
anonymous(); //set up the anonymous functions |
19 |
}
|
20 |
|
21 |
public function colorChange(event:ColorPickerEvent):void { |
22 |
ExternalInterface.call("receiveColor", event.target.hexValue); |
23 |
}
|
24 |
|
25 |
public function receiveText(value:String):void { |
26 |
jsText.text = value; |
27 |
}
|
28 |
|
29 |
public function promptClick(event:MouseEvent):void { |
30 |
promptText.text = "You said your name is: " + ExternalInterface.call("prompt", "What is your name?"); |
31 |
}
|
32 |
|
33 |
public function changeClick(event:MouseEvent):void { |
34 |
ExternalInterface.call("function(param){ document.getElementById('textChange').innerHTML = param; }", changeText.text); |
35 |
changeText.text = ""; |
36 |
}
|
37 |
|
38 |
//clear the text after one second has passed
|
39 |
public function onTimer(event:TimerEvent):void { |
40 |
resizeText.text = ""; |
41 |
timer.stop(); |
42 |
}
|
43 |
|
44 |
public function anonymous():void { |
45 |
//see how we're defining a function inside another function?
|
46 |
ExternalInterface.addCallback("anon", function(){ |
47 |
resizeText.text = "The window has been resized."; |
48 |
timer.start(); |
49 |
});
|
50 |
|
51 |
//same applies here
|
52 |
ExternalInterface.call("function(){ window.onresize = function(){ document['externalInterface'].anon(); }; }"); |
53 |
}
|
54 |
}
|
55 |
}
|
(New lines are 13-17 and 37-52.)
Conclusion
The ExternalInterface is an extremely powerful and useful class. With it, you can receive values and events, all outside of the SWF. Have fun with it and thanks for reading!