Help With Flash Javascript communication

I’m looking for help in figuring out why my flash app won’t talk to the javascript in my html page.

I got the flash to send info to the javascript reliably, but I can’t get the Javascript to send info back.

Here’s the code on my flash app:



function OnMovieLoaded():void
{
	if (ExternalInterface.available) 
	{
		try
		{
			//ExternalInterface.call("ReceiveFromFlash", "Adding callback...
","");
			
            ExternalInterface.addCallback("SendToActionScript", receivedFromJavaScript);
					
            if (checkJavaScriptReady()) 
			{            	
				//ExternalInterface.call("ReceiveFromFlash", "JavaScript is ready.
","");
			}
			else 
			{
				//ExternalInterface.call("ReceiveFromFlash", "JavaScript is not ready
","");             	                        
            }
			
		} 
		catch (error:SecurityError) 
		{
			ExternalInterface.call("ReceiveFromFlash", "A SecurityError occurred: " + error.message + "
","");        	
		} 
		catch (error:Error) 
		{
			ExternalInterface.call("ReceiveFromFlash", "An Error occurred: " + error.message + "
","");         	
        }
    } 
	else 
	{
		ExternalInterface.call("ReceiveFromFlash", "External interface is not available for this container.","");         	    	
	}
}
function receivedFromJavaScript(value:String):void 
{
	this.txf_passage.text = value;
}	



Here’s the HTML



<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Text</title>
<script language="javascript">AC_FL_RunContent = 0;</script>
<script src="AC_RunActiveContent.js" language="javascript"></script>
<script src="text.js" language="javascript"></script>
</head>
<body onLoad="javascript:OnPageLoad();" bgcolor="#ffffff">
<p>
  <!--url's used in the movie-->
  <!--text used in the movie-->
  <!--
<p align="left"></p>
<p align="center"><font face="Verdana" size="14" color="#000000" letterSpacing="0.000000" kerning="1">Please highlight the passages you like</font></p>
-->
  <!-- saved from url=(0013)about:internet -->
  <script language="javascript">
	if (AC_FL_RunContent == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0','width','650','height','450','id','Text','align','middle','src','Text','quality','high','bgcolor','#ffffff','name','Text','allowscriptaccess','sameDomain','allowfullscreen','false','pluginspage','http://www.macromedia.com/go/getflashplayer','movie','Text' ); //end AC code
	}
</script>
  <noscript>
    <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="650" height="450" id="TextMovie" align="middle">
      <param name="allowScriptAccess" value="sameDomain" />
      <param name="allowFullScreen" value="false" />
      <param name="movie" value="Text.swf" />
      <param name="quality" value="high" />
      <param name="bgcolor" value="#ffffff" />	
      <embed src="Text.swf" quality="high" bgcolor="#ffffff" width="650" height="450" name="TextMovie" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />    
  </object>
  </noscript>
</p>
<form name="form1" method="post" action="">
  <label>likes
  <textarea name="likes" cols="50" rows="10" id="likes"></textarea>
</label>
  <label>Dislikes
  <textarea name="dislikes" cols="50" rows="10" id="dislikes"></textarea>
  </label>
</form>
<p> </p>
</body>
</html>



Here’s the Javascript




var jsReady = true;

function ReceiveFromFlash(txt1,txt2) 
{	
	document.form1.likes.value = txt1;
	document.form1.dislikes.value = txt2;

	//document.getElementById('htmlText').value = txt1+" "+txt2+" "+txt3+" "+txt4;
}

function OnPageLoad() 
{
	sendToActionScript("Test");
	
	alert('Warning - codetoad causes an increased knowledge of programming and may lead to a life of coke and pizza');
}

function isReady() 
{
	return jsReady;
}

function pageInit() 
{
	jsReady = true;   
}

function thisMovie(movieName) 
{
	if (navigator.appName.indexOf("Microsoft") != -1) {
    	return window[movieName];
	} 
	else 
	{		
     	return document[movieName];
	}
}

function sendToActionScript(value) 
{
	thisMovie("TextMovie").SendToActionScript(value);
}


The onLoad function is supposed to send a string to flash, but the script quits right before it does that. In other words, the alert is never seen, unless I comment out the “sendToActionScript(“Test”);” call. As I mentioned the calls TO Javascript work fine. The “receiveFromFlash” function is called just fine by the flash app. But the “sendToActionScript” function ALWAYS fails.

Can anyone help me figure out why it isn’t working? Is it because I’m calling it on load? Am I accessing the DOM correctly?

God I hate javascript!

Well, I don’t know anything about flash programming, and I’m mediocre at best with javascript, but since no one else has answered – my first question would be, in your sendToActionScript function, what does

thisMovie(“TextMovie”)

return? alert it, or do something like:



function sendToActionScript(value) 
{
        var movie = thisMovie("TextMovie");
        alert(movie);
        if (movie) {
	   movie.SendToActionScript(value);
        } else {
          alert('Error retrieving movie: " + value);
        }
}