Here is a communication example between Flash and Javascript using (the very well known by most of you), the ExternalInterface class:

Step one:

First create an Actionscript 3.0 class, and name it ‘FlashJSCommunication.as‘:

package
<span class="br0">{</span>
<span class="coMULTI">/*
* @Author: Adrian Sule
* @Website: www.adriansule.com/blog
*/
</span>
<span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="kw3">MovieClip</span>;
<span class="kw3">import</span> flash.<span class="me1">external</span>.<span class="me1">ExternalInterface</span>;
<span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">Event</span>;
<span class="kw3">import</span> flash.<span class="kw3">text</span>.<span class="kw3">TextField</span>;
<p><span class="kw3">public</span> <span class="kw2">class</span> FlashJSCommunication <span class="kw3">extends</span> <span class="kw3">MovieClip</span>
<span class="br0">{</span>
<span class="kw3">public</span> <span class="kw2">function</span> FlashJSCommunication<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
ExternalInterface.<span class="me1">addCallback</span><span class="br0">(</span><span class="st0">"sendTextToFlash"</span>, getTextFromJavaScript<span class="br0">)</span>;
<span class="kw3">this</span>.<span class="me1">btnSendToJS</span>.<span class="me1">addEventListener</span><span class="br0">(</span><span class="st0">"click"</span>, clickSend<span class="br0">)</span>;
<span class="kw3">this</span>.<span class="me1">btnSendToJS</span>.<span class="kw3">useHandCursor</span> = <span class="kw2">true</span>;
<span class="kw3">this</span>.<span class="me1">btnSendToJS</span>.<span class="me1">buttonMode</span> = <span class="kw2">true</span>;
<span class="br0">}</span></p>
<p><span class="kw3">private</span> <span class="kw2">function</span> getTextFromJavaScript<span class="br0">(</span>str:<span class="kw3">String</span><span class="br0">)</span>:<span class="kw3">void</span>
<span class="br0">{</span>
<span class="kw3">this</span>.<span class="me1">tReceived</span>.<span class="kw3">text</span> = <span class="st0">"From JavaScript: "</span> + str;
<span class="br0">}</span></p>
<p><span class="kw3">private</span> <span class="kw2">function</span> clickSend<span class="br0">(</span>event:Event<span class="br0">)</span>:<span class="kw3">void</span>
<span class="br0">{</span>
<span class="kw2">var</span> jsArgument:<span class="kw3">String</span> = <span class="kw3">this</span>.<span class="me1">tSend</span>.<span class="kw3">text</span>;
<span class="kw2">var</span> result:<span class="kw3">Object</span> = ExternalInterface.<span class="kw3">call</span><span class="br0">(</span><span class="st0">"getTextFromFlash"</span>, jsArgument<span class="br0">)</span>;
<span class="kw3">this</span>.<span class="me1">tReceived</span>.<span class="kw3">text</span> = <span class="st0">"Returned: "</span> + result;
<span class="br0">}</span>
<span class="br0">}</span>
<span class="br0">}

Step two:

You need to create a .fla and add two input texfields named ‘tReceived’ , ‘tSend”,
and a button named ‘btnSendToJS‘.

Don’t think it’s neccesary to explain what each of this objects will do, because their instance names
are self explanatory.

Add the Document Class instance of ‘FlashJSCommunication‘.

Press CTR+SHIFT+F12 to open the Publish window, and click Publish to create an HTML for your .swf file

Step three:

Edit the HTML that contains your ‘FlashJSCommunication.swf‘ object (should be ‘FlashJSCommunication.html‘).

Add the following script in the <head> tag:

<script >
<p>function getFlashMovie(movieName)
{
var isIE = navigator.appName.indexOf(&#8221;Microsoft”) != -1;
return (isIE) ? window[movieName] : document[movieName];
}</p>
<p>function formSend()
{
var text = document.htmlForm.sendField.value;
getFlashMovie(&#8221;FlashJSCommunication”).sendTextToFlash(text);
}</p>
<p>function getTextFromFlash(str)
{
document.htmlForm.receivedField.value = &#8220;From Flash: ” + str;
return str + &#8221; received”;
}
</script>

Inside the <body> tag add this Form to get/send data from/to Flash:

<form name=&#8221;htmlForm” method=”POST” action=”javascript:formSend();”>
Sending to ActionScript:<br />
<input type=&#8221;text” name=”sendField” value=”" /><br />
<input type=&#8221;submit” value=”Send” /><br />  <br />
Received from ActionScript:<br />
<input type=&#8221;text” name=”receivedField”>
</form>

Note:
For local content running in a browser, calls to the ExternalInterface.addCallback() method work only if the SWF file and the containing web page are in the local-trusted security sandbox.

For more information, click here

Preview link

Download example